dir.c 18 KB

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