dir.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * dir.c - NILFS directory entry operations
  3. *
  4. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Modified for NILFS by Amagai Yoshiji <amagai@osrg.net>
  21. */
  22. /*
  23. * linux/fs/ext2/dir.c
  24. *
  25. * Copyright (C) 1992, 1993, 1994, 1995
  26. * Remy Card (card@masi.ibp.fr)
  27. * Laboratoire MASI - Institut Blaise Pascal
  28. * Universite Pierre et Marie Curie (Paris VI)
  29. *
  30. * from
  31. *
  32. * linux/fs/minix/dir.c
  33. *
  34. * Copyright (C) 1991, 1992 Linus Torvalds
  35. *
  36. * ext2 directory handling functions
  37. *
  38. * Big-endian to little-endian byte-swapping/bitmaps by
  39. * David S. Miller (davem@caip.rutgers.edu), 1995
  40. *
  41. * All code that works with directory layout had been switched to pagecache
  42. * and moved here. AV
  43. */
  44. #include <linux/pagemap.h>
  45. #include "nilfs.h"
  46. #include "page.h"
  47. /*
  48. * nilfs uses block-sized chunks. Arguably, sector-sized ones would be
  49. * more robust, but we have what we have
  50. */
  51. static inline unsigned nilfs_chunk_size(struct inode *inode)
  52. {
  53. return inode->i_sb->s_blocksize;
  54. }
  55. static inline void nilfs_put_page(struct page *page)
  56. {
  57. kunmap(page);
  58. page_cache_release(page);
  59. }
  60. static inline unsigned long dir_pages(struct inode *inode)
  61. {
  62. return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
  63. }
  64. /*
  65. * Return the offset into page `page_nr' of the last valid
  66. * byte in that page, plus one.
  67. */
  68. static unsigned nilfs_last_byte(struct inode *inode, unsigned long page_nr)
  69. {
  70. unsigned last_byte = inode->i_size;
  71. last_byte -= page_nr << PAGE_CACHE_SHIFT;
  72. if (last_byte > PAGE_CACHE_SIZE)
  73. last_byte = PAGE_CACHE_SIZE;
  74. return last_byte;
  75. }
  76. static int nilfs_prepare_chunk(struct page *page, unsigned from, unsigned to)
  77. {
  78. loff_t pos = page_offset(page) + from;
  79. return __block_write_begin(page, pos, to - from, nilfs_get_block);
  80. }
  81. static void nilfs_commit_chunk(struct page *page,
  82. struct address_space *mapping,
  83. unsigned from, unsigned to)
  84. {
  85. struct inode *dir = mapping->host;
  86. loff_t pos = page_offset(page) + from;
  87. unsigned len = to - from;
  88. unsigned nr_dirty, copied;
  89. int err;
  90. nr_dirty = nilfs_page_count_clean_buffers(page, from, to);
  91. copied = block_write_end(NULL, mapping, pos, len, len, page, NULL);
  92. if (pos + copied > dir->i_size)
  93. i_size_write(dir, pos + copied);
  94. if (IS_DIRSYNC(dir))
  95. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  96. err = nilfs_set_file_dirty(dir, nr_dirty);
  97. WARN_ON(err); /* do not happen */
  98. unlock_page(page);
  99. }
  100. static void nilfs_check_page(struct page *page)
  101. {
  102. struct inode *dir = page->mapping->host;
  103. struct super_block *sb = dir->i_sb;
  104. unsigned chunk_size = nilfs_chunk_size(dir);
  105. char *kaddr = page_address(page);
  106. unsigned offs, rec_len;
  107. unsigned limit = PAGE_CACHE_SIZE;
  108. struct nilfs_dir_entry *p;
  109. char *error;
  110. if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
  111. limit = dir->i_size & ~PAGE_CACHE_MASK;
  112. if (limit & (chunk_size - 1))
  113. goto Ebadsize;
  114. if (!limit)
  115. goto out;
  116. }
  117. for (offs = 0; offs <= limit - NILFS_DIR_REC_LEN(1); offs += rec_len) {
  118. p = (struct nilfs_dir_entry *)(kaddr + offs);
  119. rec_len = nilfs_rec_len_from_disk(p->rec_len);
  120. if (rec_len < NILFS_DIR_REC_LEN(1))
  121. goto Eshort;
  122. if (rec_len & 3)
  123. goto Ealign;
  124. if (rec_len < NILFS_DIR_REC_LEN(p->name_len))
  125. goto Enamelen;
  126. if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
  127. goto Espan;
  128. }
  129. if (offs != limit)
  130. goto Eend;
  131. out:
  132. SetPageChecked(page);
  133. return;
  134. /* Too bad, we had an error */
  135. Ebadsize:
  136. nilfs_error(sb, "nilfs_check_page",
  137. "size of directory #%lu is not a multiple of chunk size",
  138. dir->i_ino
  139. );
  140. goto fail;
  141. Eshort:
  142. error = "rec_len is smaller than minimal";
  143. goto bad_entry;
  144. Ealign:
  145. error = "unaligned directory entry";
  146. goto bad_entry;
  147. Enamelen:
  148. error = "rec_len is too small for name_len";
  149. goto bad_entry;
  150. Espan:
  151. error = "directory entry across blocks";
  152. bad_entry:
  153. nilfs_error(sb, "nilfs_check_page", "bad entry in directory #%lu: %s - "
  154. "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
  155. dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
  156. (unsigned long) le64_to_cpu(p->inode),
  157. rec_len, p->name_len);
  158. goto fail;
  159. Eend:
  160. p = (struct nilfs_dir_entry *)(kaddr + offs);
  161. nilfs_error(sb, "nilfs_check_page",
  162. "entry in directory #%lu spans the page boundary"
  163. "offset=%lu, inode=%lu",
  164. dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
  165. (unsigned long) le64_to_cpu(p->inode));
  166. fail:
  167. SetPageChecked(page);
  168. SetPageError(page);
  169. }
  170. static struct page *nilfs_get_page(struct inode *dir, unsigned long n)
  171. {
  172. struct address_space *mapping = dir->i_mapping;
  173. struct page *page = read_mapping_page(mapping, n, NULL);
  174. if (!IS_ERR(page)) {
  175. kmap(page);
  176. if (!PageChecked(page))
  177. nilfs_check_page(page);
  178. if (PageError(page))
  179. goto fail;
  180. }
  181. return page;
  182. fail:
  183. nilfs_put_page(page);
  184. return ERR_PTR(-EIO);
  185. }
  186. /*
  187. * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure.
  188. *
  189. * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller.
  190. */
  191. static int
  192. nilfs_match(int len, const unsigned char *name, struct nilfs_dir_entry *de)
  193. {
  194. if (len != de->name_len)
  195. return 0;
  196. if (!de->inode)
  197. return 0;
  198. return !memcmp(name, de->name, len);
  199. }
  200. /*
  201. * p is at least 6 bytes before the end of page
  202. */
  203. static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p)
  204. {
  205. return (struct nilfs_dir_entry *)((char *)p +
  206. nilfs_rec_len_from_disk(p->rec_len));
  207. }
  208. static unsigned char
  209. nilfs_filetype_table[NILFS_FT_MAX] = {
  210. [NILFS_FT_UNKNOWN] = DT_UNKNOWN,
  211. [NILFS_FT_REG_FILE] = DT_REG,
  212. [NILFS_FT_DIR] = DT_DIR,
  213. [NILFS_FT_CHRDEV] = DT_CHR,
  214. [NILFS_FT_BLKDEV] = DT_BLK,
  215. [NILFS_FT_FIFO] = DT_FIFO,
  216. [NILFS_FT_SOCK] = DT_SOCK,
  217. [NILFS_FT_SYMLINK] = DT_LNK,
  218. };
  219. #define S_SHIFT 12
  220. static unsigned char
  221. nilfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  222. [S_IFREG >> S_SHIFT] = NILFS_FT_REG_FILE,
  223. [S_IFDIR >> S_SHIFT] = NILFS_FT_DIR,
  224. [S_IFCHR >> S_SHIFT] = NILFS_FT_CHRDEV,
  225. [S_IFBLK >> S_SHIFT] = NILFS_FT_BLKDEV,
  226. [S_IFIFO >> S_SHIFT] = NILFS_FT_FIFO,
  227. [S_IFSOCK >> S_SHIFT] = NILFS_FT_SOCK,
  228. [S_IFLNK >> S_SHIFT] = NILFS_FT_SYMLINK,
  229. };
  230. static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode)
  231. {
  232. umode_t mode = inode->i_mode;
  233. de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
  234. }
  235. static int nilfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  236. {
  237. loff_t pos = filp->f_pos;
  238. struct inode *inode = filp->f_dentry->d_inode;
  239. struct super_block *sb = inode->i_sb;
  240. unsigned int offset = pos & ~PAGE_CACHE_MASK;
  241. unsigned long n = pos >> PAGE_CACHE_SHIFT;
  242. unsigned long npages = dir_pages(inode);
  243. /* unsigned chunk_mask = ~(nilfs_chunk_size(inode)-1); */
  244. unsigned char *types = NULL;
  245. int ret;
  246. if (pos > inode->i_size - NILFS_DIR_REC_LEN(1))
  247. goto success;
  248. types = nilfs_filetype_table;
  249. for ( ; n < npages; n++, offset = 0) {
  250. char *kaddr, *limit;
  251. struct nilfs_dir_entry *de;
  252. struct page *page = nilfs_get_page(inode, n);
  253. if (IS_ERR(page)) {
  254. nilfs_error(sb, __func__, "bad page in #%lu",
  255. inode->i_ino);
  256. filp->f_pos += PAGE_CACHE_SIZE - offset;
  257. ret = -EIO;
  258. goto done;
  259. }
  260. kaddr = page_address(page);
  261. de = (struct nilfs_dir_entry *)(kaddr + offset);
  262. limit = kaddr + nilfs_last_byte(inode, n) -
  263. NILFS_DIR_REC_LEN(1);
  264. for ( ; (char *)de <= limit; de = nilfs_next_entry(de)) {
  265. if (de->rec_len == 0) {
  266. nilfs_error(sb, __func__,
  267. "zero-length directory entry");
  268. ret = -EIO;
  269. nilfs_put_page(page);
  270. goto done;
  271. }
  272. if (de->inode) {
  273. int over;
  274. unsigned char d_type = DT_UNKNOWN;
  275. if (types && de->file_type < NILFS_FT_MAX)
  276. d_type = types[de->file_type];
  277. offset = (char *)de - kaddr;
  278. over = filldir(dirent, de->name, de->name_len,
  279. (n<<PAGE_CACHE_SHIFT) | offset,
  280. le64_to_cpu(de->inode), d_type);
  281. if (over) {
  282. nilfs_put_page(page);
  283. goto success;
  284. }
  285. }
  286. filp->f_pos += nilfs_rec_len_from_disk(de->rec_len);
  287. }
  288. nilfs_put_page(page);
  289. }
  290. success:
  291. ret = 0;
  292. done:
  293. return ret;
  294. }
  295. /*
  296. * nilfs_find_entry()
  297. *
  298. * finds an entry in the specified directory with the wanted name. It
  299. * returns the page in which the entry was found, and the entry itself
  300. * (as a parameter - res_dir). Page is returned mapped and unlocked.
  301. * Entry is guaranteed to be valid.
  302. */
  303. struct nilfs_dir_entry *
  304. nilfs_find_entry(struct inode *dir, const struct qstr *qstr,
  305. struct page **res_page)
  306. {
  307. const unsigned char *name = qstr->name;
  308. int namelen = qstr->len;
  309. unsigned reclen = NILFS_DIR_REC_LEN(namelen);
  310. unsigned long start, n;
  311. unsigned long npages = dir_pages(dir);
  312. struct page *page = NULL;
  313. struct nilfs_inode_info *ei = NILFS_I(dir);
  314. struct nilfs_dir_entry *de;
  315. if (npages == 0)
  316. goto out;
  317. /* OFFSET_CACHE */
  318. *res_page = NULL;
  319. start = ei->i_dir_start_lookup;
  320. if (start >= npages)
  321. start = 0;
  322. n = start;
  323. do {
  324. char *kaddr;
  325. page = nilfs_get_page(dir, n);
  326. if (!IS_ERR(page)) {
  327. kaddr = page_address(page);
  328. de = (struct nilfs_dir_entry *)kaddr;
  329. kaddr += nilfs_last_byte(dir, n) - reclen;
  330. while ((char *) de <= kaddr) {
  331. if (de->rec_len == 0) {
  332. nilfs_error(dir->i_sb, __func__,
  333. "zero-length directory entry");
  334. nilfs_put_page(page);
  335. goto out;
  336. }
  337. if (nilfs_match(namelen, name, de))
  338. goto found;
  339. de = nilfs_next_entry(de);
  340. }
  341. nilfs_put_page(page);
  342. }
  343. if (++n >= npages)
  344. n = 0;
  345. /* next page is past the blocks we've got */
  346. if (unlikely(n > (dir->i_blocks >> (PAGE_CACHE_SHIFT - 9)))) {
  347. nilfs_error(dir->i_sb, __func__,
  348. "dir %lu size %lld exceeds block count %llu",
  349. dir->i_ino, dir->i_size,
  350. (unsigned long long)dir->i_blocks);
  351. goto out;
  352. }
  353. } while (n != start);
  354. out:
  355. return NULL;
  356. found:
  357. *res_page = page;
  358. ei->i_dir_start_lookup = n;
  359. return de;
  360. }
  361. struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct page **p)
  362. {
  363. struct page *page = nilfs_get_page(dir, 0);
  364. struct nilfs_dir_entry *de = NULL;
  365. if (!IS_ERR(page)) {
  366. de = nilfs_next_entry(
  367. (struct nilfs_dir_entry *)page_address(page));
  368. *p = page;
  369. }
  370. return de;
  371. }
  372. ino_t nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr)
  373. {
  374. ino_t res = 0;
  375. struct nilfs_dir_entry *de;
  376. struct page *page;
  377. de = nilfs_find_entry(dir, qstr, &page);
  378. if (de) {
  379. res = le64_to_cpu(de->inode);
  380. kunmap(page);
  381. page_cache_release(page);
  382. }
  383. return res;
  384. }
  385. /* Releases the page */
  386. void nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de,
  387. struct page *page, struct inode *inode)
  388. {
  389. unsigned from = (char *) de - (char *) page_address(page);
  390. unsigned to = from + nilfs_rec_len_from_disk(de->rec_len);
  391. struct address_space *mapping = page->mapping;
  392. int err;
  393. lock_page(page);
  394. err = nilfs_prepare_chunk(page, from, to);
  395. BUG_ON(err);
  396. de->inode = cpu_to_le64(inode->i_ino);
  397. nilfs_set_de_type(de, inode);
  398. nilfs_commit_chunk(page, mapping, from, to);
  399. nilfs_put_page(page);
  400. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  401. }
  402. /*
  403. * Parent is locked.
  404. */
  405. int nilfs_add_link(struct dentry *dentry, struct inode *inode)
  406. {
  407. struct inode *dir = dentry->d_parent->d_inode;
  408. const unsigned char *name = dentry->d_name.name;
  409. int namelen = dentry->d_name.len;
  410. unsigned chunk_size = nilfs_chunk_size(dir);
  411. unsigned reclen = NILFS_DIR_REC_LEN(namelen);
  412. unsigned short rec_len, name_len;
  413. struct page *page = NULL;
  414. struct nilfs_dir_entry *de;
  415. unsigned long npages = dir_pages(dir);
  416. unsigned long n;
  417. char *kaddr;
  418. unsigned from, to;
  419. int err;
  420. /*
  421. * We take care of directory expansion in the same loop.
  422. * This code plays outside i_size, so it locks the page
  423. * to protect that region.
  424. */
  425. for (n = 0; n <= npages; n++) {
  426. char *dir_end;
  427. page = nilfs_get_page(dir, n);
  428. err = PTR_ERR(page);
  429. if (IS_ERR(page))
  430. goto out;
  431. lock_page(page);
  432. kaddr = page_address(page);
  433. dir_end = kaddr + nilfs_last_byte(dir, n);
  434. de = (struct nilfs_dir_entry *)kaddr;
  435. kaddr += PAGE_CACHE_SIZE - reclen;
  436. while ((char *)de <= kaddr) {
  437. if ((char *)de == dir_end) {
  438. /* We hit i_size */
  439. name_len = 0;
  440. rec_len = chunk_size;
  441. de->rec_len = nilfs_rec_len_to_disk(chunk_size);
  442. de->inode = 0;
  443. goto got_it;
  444. }
  445. if (de->rec_len == 0) {
  446. nilfs_error(dir->i_sb, __func__,
  447. "zero-length directory entry");
  448. err = -EIO;
  449. goto out_unlock;
  450. }
  451. err = -EEXIST;
  452. if (nilfs_match(namelen, name, de))
  453. goto out_unlock;
  454. name_len = NILFS_DIR_REC_LEN(de->name_len);
  455. rec_len = nilfs_rec_len_from_disk(de->rec_len);
  456. if (!de->inode && rec_len >= reclen)
  457. goto got_it;
  458. if (rec_len >= name_len + reclen)
  459. goto got_it;
  460. de = (struct nilfs_dir_entry *)((char *)de + rec_len);
  461. }
  462. unlock_page(page);
  463. nilfs_put_page(page);
  464. }
  465. BUG();
  466. return -EINVAL;
  467. got_it:
  468. from = (char *)de - (char *)page_address(page);
  469. to = from + rec_len;
  470. err = nilfs_prepare_chunk(page, from, to);
  471. if (err)
  472. goto out_unlock;
  473. if (de->inode) {
  474. struct nilfs_dir_entry *de1;
  475. de1 = (struct nilfs_dir_entry *)((char *)de + name_len);
  476. de1->rec_len = nilfs_rec_len_to_disk(rec_len - name_len);
  477. de->rec_len = nilfs_rec_len_to_disk(name_len);
  478. de = de1;
  479. }
  480. de->name_len = namelen;
  481. memcpy(de->name, name, namelen);
  482. de->inode = cpu_to_le64(inode->i_ino);
  483. nilfs_set_de_type(de, inode);
  484. nilfs_commit_chunk(page, page->mapping, from, to);
  485. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  486. nilfs_mark_inode_dirty(dir);
  487. /* OFFSET_CACHE */
  488. out_put:
  489. nilfs_put_page(page);
  490. out:
  491. return err;
  492. out_unlock:
  493. unlock_page(page);
  494. goto out_put;
  495. }
  496. /*
  497. * nilfs_delete_entry deletes a directory entry by merging it with the
  498. * previous entry. Page is up-to-date. Releases the page.
  499. */
  500. int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct page *page)
  501. {
  502. struct address_space *mapping = page->mapping;
  503. struct inode *inode = mapping->host;
  504. char *kaddr = page_address(page);
  505. unsigned from = ((char *)dir - kaddr) & ~(nilfs_chunk_size(inode) - 1);
  506. unsigned to = ((char *)dir - kaddr) +
  507. nilfs_rec_len_from_disk(dir->rec_len);
  508. struct nilfs_dir_entry *pde = NULL;
  509. struct nilfs_dir_entry *de = (struct nilfs_dir_entry *)(kaddr + from);
  510. int err;
  511. while ((char *)de < (char *)dir) {
  512. if (de->rec_len == 0) {
  513. nilfs_error(inode->i_sb, __func__,
  514. "zero-length directory entry");
  515. err = -EIO;
  516. goto out;
  517. }
  518. pde = de;
  519. de = nilfs_next_entry(de);
  520. }
  521. if (pde)
  522. from = (char *)pde - (char *)page_address(page);
  523. lock_page(page);
  524. err = nilfs_prepare_chunk(page, from, to);
  525. BUG_ON(err);
  526. if (pde)
  527. pde->rec_len = nilfs_rec_len_to_disk(to - from);
  528. dir->inode = 0;
  529. nilfs_commit_chunk(page, mapping, from, to);
  530. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  531. out:
  532. nilfs_put_page(page);
  533. return err;
  534. }
  535. /*
  536. * Set the first fragment of directory.
  537. */
  538. int nilfs_make_empty(struct inode *inode, struct inode *parent)
  539. {
  540. struct address_space *mapping = inode->i_mapping;
  541. struct page *page = grab_cache_page(mapping, 0);
  542. unsigned chunk_size = nilfs_chunk_size(inode);
  543. struct nilfs_dir_entry *de;
  544. int err;
  545. void *kaddr;
  546. if (!page)
  547. return -ENOMEM;
  548. err = nilfs_prepare_chunk(page, 0, chunk_size);
  549. if (unlikely(err)) {
  550. unlock_page(page);
  551. goto fail;
  552. }
  553. kaddr = kmap_atomic(page);
  554. memset(kaddr, 0, chunk_size);
  555. de = (struct nilfs_dir_entry *)kaddr;
  556. de->name_len = 1;
  557. de->rec_len = nilfs_rec_len_to_disk(NILFS_DIR_REC_LEN(1));
  558. memcpy(de->name, ".\0\0", 4);
  559. de->inode = cpu_to_le64(inode->i_ino);
  560. nilfs_set_de_type(de, inode);
  561. de = (struct nilfs_dir_entry *)(kaddr + NILFS_DIR_REC_LEN(1));
  562. de->name_len = 2;
  563. de->rec_len = nilfs_rec_len_to_disk(chunk_size - NILFS_DIR_REC_LEN(1));
  564. de->inode = cpu_to_le64(parent->i_ino);
  565. memcpy(de->name, "..\0", 4);
  566. nilfs_set_de_type(de, inode);
  567. kunmap_atomic(kaddr);
  568. nilfs_commit_chunk(page, mapping, 0, chunk_size);
  569. fail:
  570. page_cache_release(page);
  571. return err;
  572. }
  573. /*
  574. * routine to check that the specified directory is empty (for rmdir)
  575. */
  576. int nilfs_empty_dir(struct inode *inode)
  577. {
  578. struct page *page = NULL;
  579. unsigned long i, npages = dir_pages(inode);
  580. for (i = 0; i < npages; i++) {
  581. char *kaddr;
  582. struct nilfs_dir_entry *de;
  583. page = nilfs_get_page(inode, i);
  584. if (IS_ERR(page))
  585. continue;
  586. kaddr = page_address(page);
  587. de = (struct nilfs_dir_entry *)kaddr;
  588. kaddr += nilfs_last_byte(inode, i) - NILFS_DIR_REC_LEN(1);
  589. while ((char *)de <= kaddr) {
  590. if (de->rec_len == 0) {
  591. nilfs_error(inode->i_sb, __func__,
  592. "zero-length directory entry "
  593. "(kaddr=%p, de=%p)\n", kaddr, de);
  594. goto not_empty;
  595. }
  596. if (de->inode != 0) {
  597. /* check for . and .. */
  598. if (de->name[0] != '.')
  599. goto not_empty;
  600. if (de->name_len > 2)
  601. goto not_empty;
  602. if (de->name_len < 2) {
  603. if (de->inode !=
  604. cpu_to_le64(inode->i_ino))
  605. goto not_empty;
  606. } else if (de->name[1] != '.')
  607. goto not_empty;
  608. }
  609. de = nilfs_next_entry(de);
  610. }
  611. nilfs_put_page(page);
  612. }
  613. return 1;
  614. not_empty:
  615. nilfs_put_page(page);
  616. return 0;
  617. }
  618. const struct file_operations nilfs_dir_operations = {
  619. .llseek = generic_file_llseek,
  620. .read = generic_read_dir,
  621. .readdir = nilfs_readdir,
  622. .unlocked_ioctl = nilfs_ioctl,
  623. #ifdef CONFIG_COMPAT
  624. .compat_ioctl = nilfs_compat_ioctl,
  625. #endif /* CONFIG_COMPAT */
  626. .fsync = nilfs_sync_file,
  627. };