dir.c 18 KB

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