dir.c 17 KB

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