dir.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. /*
  63. * Return the offset into page `page_nr' of the last valid
  64. * byte in that page, plus one.
  65. */
  66. static unsigned
  67. ext2_last_byte(struct inode *inode, unsigned long page_nr)
  68. {
  69. unsigned last_byte = inode->i_size;
  70. last_byte -= page_nr << PAGE_SHIFT;
  71. if (last_byte > PAGE_SIZE)
  72. last_byte = PAGE_SIZE;
  73. return last_byte;
  74. }
  75. static void ext2_commit_chunk(struct folio *folio, loff_t pos, unsigned len)
  76. {
  77. struct address_space *mapping = folio->mapping;
  78. struct inode *dir = mapping->host;
  79. inode_inc_iversion(dir);
  80. block_write_end(NULL, mapping, pos, len, len, folio, NULL);
  81. if (pos+len > dir->i_size) {
  82. i_size_write(dir, pos+len);
  83. mark_inode_dirty(dir);
  84. }
  85. folio_unlock(folio);
  86. }
  87. static bool ext2_check_folio(struct folio *folio, int quiet, char *kaddr)
  88. {
  89. struct inode *dir = folio->mapping->host;
  90. struct super_block *sb = dir->i_sb;
  91. unsigned chunk_size = ext2_chunk_size(dir);
  92. u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count);
  93. unsigned offs, rec_len;
  94. unsigned limit = folio_size(folio);
  95. ext2_dirent *p;
  96. char *error;
  97. if (dir->i_size < folio_pos(folio) + limit) {
  98. limit = offset_in_folio(folio, dir->i_size);
  99. if (limit & (chunk_size - 1))
  100. goto Ebadsize;
  101. if (!limit)
  102. goto out;
  103. }
  104. for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
  105. p = (ext2_dirent *)(kaddr + offs);
  106. rec_len = ext2_rec_len_from_disk(p->rec_len);
  107. if (unlikely(rec_len < EXT2_DIR_REC_LEN(1)))
  108. goto Eshort;
  109. if (unlikely(rec_len & 3))
  110. goto Ealign;
  111. if (unlikely(rec_len < EXT2_DIR_REC_LEN(p->name_len)))
  112. goto Enamelen;
  113. if (unlikely(((offs + rec_len - 1) ^ offs) & ~(chunk_size-1)))
  114. goto Espan;
  115. if (unlikely(le32_to_cpu(p->inode) > max_inumber))
  116. goto Einumber;
  117. }
  118. if (offs != limit)
  119. goto Eend;
  120. out:
  121. folio_set_checked(folio);
  122. return true;
  123. /* Too bad, we had an error */
  124. Ebadsize:
  125. if (!quiet)
  126. ext2_error(sb, __func__,
  127. "size of directory #%lu is not a multiple "
  128. "of chunk size", dir->i_ino);
  129. goto fail;
  130. Eshort:
  131. error = "rec_len is smaller than minimal";
  132. goto bad_entry;
  133. Ealign:
  134. error = "unaligned directory entry";
  135. goto bad_entry;
  136. Enamelen:
  137. error = "rec_len is too small for name_len";
  138. goto bad_entry;
  139. Espan:
  140. error = "directory entry across blocks";
  141. goto bad_entry;
  142. Einumber:
  143. error = "inode out of bounds";
  144. bad_entry:
  145. if (!quiet)
  146. ext2_error(sb, __func__, "bad entry in directory #%lu: : %s - "
  147. "offset=%llu, inode=%lu, rec_len=%d, name_len=%d",
  148. dir->i_ino, error, folio_pos(folio) + offs,
  149. (unsigned long) le32_to_cpu(p->inode),
  150. rec_len, p->name_len);
  151. goto fail;
  152. Eend:
  153. if (!quiet) {
  154. p = (ext2_dirent *)(kaddr + offs);
  155. ext2_error(sb, "ext2_check_folio",
  156. "entry in directory #%lu spans the page boundary"
  157. "offset=%llu, inode=%lu",
  158. dir->i_ino, folio_pos(folio) + offs,
  159. (unsigned long) le32_to_cpu(p->inode));
  160. }
  161. fail:
  162. return false;
  163. }
  164. /*
  165. * Calls to ext2_get_folio()/folio_release_kmap() must be nested according
  166. * to the rules documented in kmap_local_folio()/kunmap_local().
  167. *
  168. * NOTE: ext2_find_entry() and ext2_dotdot() act as a call
  169. * to folio_release_kmap() and should be treated as a call to
  170. * folio_release_kmap() for nesting purposes.
  171. */
  172. static void *ext2_get_folio(struct inode *dir, unsigned long n,
  173. int quiet, struct folio **foliop)
  174. {
  175. struct address_space *mapping = dir->i_mapping;
  176. struct folio *folio = read_mapping_folio(mapping, n, NULL);
  177. void *kaddr;
  178. if (IS_ERR(folio))
  179. return ERR_CAST(folio);
  180. kaddr = kmap_local_folio(folio, 0);
  181. if (unlikely(!folio_test_checked(folio))) {
  182. if (!ext2_check_folio(folio, quiet, kaddr))
  183. goto fail;
  184. }
  185. *foliop = folio;
  186. return kaddr;
  187. fail:
  188. folio_release_kmap(folio, kaddr);
  189. return ERR_PTR(-EIO);
  190. }
  191. /*
  192. * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
  193. *
  194. * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
  195. */
  196. static inline int ext2_match (int len, const char * const name,
  197. struct ext2_dir_entry_2 * de)
  198. {
  199. if (len != de->name_len)
  200. return 0;
  201. if (!de->inode)
  202. return 0;
  203. return !memcmp(name, de->name, len);
  204. }
  205. /*
  206. * p is at least 6 bytes before the end of page
  207. */
  208. static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
  209. {
  210. return (ext2_dirent *)((char *)p +
  211. ext2_rec_len_from_disk(p->rec_len));
  212. }
  213. static inline unsigned
  214. ext2_validate_entry(char *base, unsigned offset, unsigned mask)
  215. {
  216. ext2_dirent *de = (ext2_dirent*)(base + offset);
  217. ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
  218. while ((char*)p < (char*)de) {
  219. if (p->rec_len == 0)
  220. break;
  221. p = ext2_next_entry(p);
  222. }
  223. return offset_in_page(p);
  224. }
  225. static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
  226. {
  227. if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
  228. de->file_type = fs_umode_to_ftype(inode->i_mode);
  229. else
  230. de->file_type = 0;
  231. }
  232. static int
  233. ext2_readdir(struct file *file, struct dir_context *ctx)
  234. {
  235. loff_t pos = ctx->pos;
  236. struct inode *inode = file_inode(file);
  237. struct super_block *sb = inode->i_sb;
  238. unsigned int offset = pos & ~PAGE_MASK;
  239. unsigned long n = pos >> PAGE_SHIFT;
  240. unsigned long npages = dir_pages(inode);
  241. unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
  242. bool need_revalidate = !inode_eq_iversion(inode, *(u64 *)file->private_data);
  243. bool has_filetype;
  244. if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
  245. return 0;
  246. has_filetype =
  247. EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE);
  248. for ( ; n < npages; n++, offset = 0) {
  249. ext2_dirent *de;
  250. struct folio *folio;
  251. char *kaddr = ext2_get_folio(inode, n, 0, &folio);
  252. char *limit;
  253. if (IS_ERR(kaddr)) {
  254. ext2_error(sb, __func__,
  255. "bad page in #%lu",
  256. inode->i_ino);
  257. ctx->pos += PAGE_SIZE - offset;
  258. return PTR_ERR(kaddr);
  259. }
  260. if (unlikely(need_revalidate)) {
  261. if (offset) {
  262. offset = ext2_validate_entry(kaddr, offset, chunk_mask);
  263. ctx->pos = (n<<PAGE_SHIFT) + offset;
  264. }
  265. *(u64 *)file->private_data = inode_query_iversion(inode);
  266. need_revalidate = false;
  267. }
  268. de = (ext2_dirent *)(kaddr+offset);
  269. limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1);
  270. for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
  271. if (de->rec_len == 0) {
  272. ext2_error(sb, __func__,
  273. "zero-length directory entry");
  274. folio_release_kmap(folio, de);
  275. return -EIO;
  276. }
  277. if (de->inode) {
  278. unsigned char d_type = DT_UNKNOWN;
  279. if (has_filetype)
  280. d_type = fs_ftype_to_dtype(de->file_type);
  281. if (!dir_emit(ctx, de->name, de->name_len,
  282. le32_to_cpu(de->inode),
  283. d_type)) {
  284. folio_release_kmap(folio, de);
  285. return 0;
  286. }
  287. }
  288. ctx->pos += ext2_rec_len_from_disk(de->rec_len);
  289. }
  290. folio_release_kmap(folio, kaddr);
  291. }
  292. return 0;
  293. }
  294. /*
  295. * ext2_find_entry()
  296. *
  297. * finds an entry in the specified directory with the wanted name. It
  298. * returns the page in which the entry was found (as a parameter - res_page),
  299. * and the entry itself. Page is returned mapped and unlocked.
  300. * Entry is guaranteed to be valid.
  301. *
  302. * On Success folio_release_kmap() should be called on *foliop.
  303. *
  304. * NOTE: Calls to ext2_get_folio()/folio_release_kmap() must be nested
  305. * according to the rules documented in kmap_local_folio()/kunmap_local().
  306. *
  307. * ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_folio()
  308. * and should be treated as a call to ext2_get_folio() for nesting
  309. * purposes.
  310. */
  311. struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir,
  312. const struct qstr *child, struct folio **foliop)
  313. {
  314. const char *name = child->name;
  315. int namelen = child->len;
  316. unsigned reclen = EXT2_DIR_REC_LEN(namelen);
  317. unsigned long start, n;
  318. unsigned long npages = dir_pages(dir);
  319. struct ext2_inode_info *ei = EXT2_I(dir);
  320. ext2_dirent * de;
  321. if (npages == 0)
  322. goto out;
  323. start = ei->i_dir_start_lookup;
  324. if (start >= npages)
  325. start = 0;
  326. n = start;
  327. do {
  328. char *kaddr = ext2_get_folio(dir, n, 0, foliop);
  329. if (IS_ERR(kaddr))
  330. return ERR_CAST(kaddr);
  331. de = (ext2_dirent *) kaddr;
  332. kaddr += ext2_last_byte(dir, n) - reclen;
  333. while ((char *) de <= kaddr) {
  334. if (de->rec_len == 0) {
  335. ext2_error(dir->i_sb, __func__,
  336. "zero-length directory entry");
  337. folio_release_kmap(*foliop, de);
  338. goto out;
  339. }
  340. if (ext2_match(namelen, name, de))
  341. goto found;
  342. de = ext2_next_entry(de);
  343. }
  344. folio_release_kmap(*foliop, kaddr);
  345. if (++n >= npages)
  346. n = 0;
  347. /* next folio is past the blocks we've got */
  348. if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
  349. ext2_error(dir->i_sb, __func__,
  350. "dir %lu size %lld exceeds block count %llu",
  351. dir->i_ino, dir->i_size,
  352. (unsigned long long)dir->i_blocks);
  353. goto out;
  354. }
  355. } while (n != start);
  356. out:
  357. return ERR_PTR(-ENOENT);
  358. found:
  359. ei->i_dir_start_lookup = n;
  360. return de;
  361. }
  362. /*
  363. * Return the '..' directory entry and the page in which the entry was found
  364. * (as a parameter - p).
  365. *
  366. * On Success folio_release_kmap() should be called on *foliop.
  367. *
  368. * NOTE: Calls to ext2_get_folio()/folio_release_kmap() must be nested
  369. * according to the rules documented in kmap_local_folio()/kunmap_local().
  370. *
  371. * ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_folio()
  372. * and should be treated as a call to ext2_get_folio() for nesting
  373. * purposes.
  374. */
  375. struct ext2_dir_entry_2 *ext2_dotdot(struct inode *dir, struct folio **foliop)
  376. {
  377. ext2_dirent *de = ext2_get_folio(dir, 0, 0, foliop);
  378. if (!IS_ERR(de))
  379. return ext2_next_entry(de);
  380. return NULL;
  381. }
  382. int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino)
  383. {
  384. struct ext2_dir_entry_2 *de;
  385. struct folio *folio;
  386. de = ext2_find_entry(dir, child, &folio);
  387. if (IS_ERR(de))
  388. return PTR_ERR(de);
  389. *ino = le32_to_cpu(de->inode);
  390. folio_release_kmap(folio, de);
  391. return 0;
  392. }
  393. static int ext2_prepare_chunk(struct folio *folio, loff_t pos, unsigned len)
  394. {
  395. return __block_write_begin(folio, pos, len, ext2_get_block);
  396. }
  397. static int ext2_handle_dirsync(struct inode *dir)
  398. {
  399. int err;
  400. err = filemap_write_and_wait(dir->i_mapping);
  401. if (!err)
  402. err = sync_inode_metadata(dir, 1);
  403. return err;
  404. }
  405. int ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
  406. struct folio *folio, struct inode *inode, bool update_times)
  407. {
  408. loff_t pos = folio_pos(folio) + offset_in_folio(folio, de);
  409. unsigned len = ext2_rec_len_from_disk(de->rec_len);
  410. int err;
  411. folio_lock(folio);
  412. err = ext2_prepare_chunk(folio, pos, len);
  413. if (err) {
  414. folio_unlock(folio);
  415. return err;
  416. }
  417. de->inode = cpu_to_le32(inode->i_ino);
  418. ext2_set_de_type(de, inode);
  419. ext2_commit_chunk(folio, pos, len);
  420. if (update_times)
  421. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  422. EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
  423. mark_inode_dirty(dir);
  424. return ext2_handle_dirsync(dir);
  425. }
  426. /*
  427. * Parent is locked.
  428. */
  429. int ext2_add_link (struct dentry *dentry, struct inode *inode)
  430. {
  431. struct inode *dir = d_inode(dentry->d_parent);
  432. const char *name = dentry->d_name.name;
  433. int namelen = dentry->d_name.len;
  434. unsigned chunk_size = ext2_chunk_size(dir);
  435. unsigned reclen = EXT2_DIR_REC_LEN(namelen);
  436. unsigned short rec_len, name_len;
  437. struct folio *folio = NULL;
  438. ext2_dirent * de;
  439. unsigned long npages = dir_pages(dir);
  440. unsigned long n;
  441. loff_t pos;
  442. int err;
  443. /*
  444. * We take care of directory expansion in the same loop.
  445. * This code plays outside i_size, so it locks the folio
  446. * to protect that region.
  447. */
  448. for (n = 0; n <= npages; n++) {
  449. char *kaddr = ext2_get_folio(dir, n, 0, &folio);
  450. char *dir_end;
  451. if (IS_ERR(kaddr))
  452. return PTR_ERR(kaddr);
  453. folio_lock(folio);
  454. dir_end = kaddr + ext2_last_byte(dir, n);
  455. de = (ext2_dirent *)kaddr;
  456. kaddr += folio_size(folio) - reclen;
  457. while ((char *)de <= kaddr) {
  458. if ((char *)de == dir_end) {
  459. /* We hit i_size */
  460. name_len = 0;
  461. rec_len = chunk_size;
  462. de->rec_len = ext2_rec_len_to_disk(chunk_size);
  463. de->inode = 0;
  464. goto got_it;
  465. }
  466. if (de->rec_len == 0) {
  467. ext2_error(dir->i_sb, __func__,
  468. "zero-length directory entry");
  469. err = -EIO;
  470. goto out_unlock;
  471. }
  472. err = -EEXIST;
  473. if (ext2_match (namelen, name, de))
  474. goto out_unlock;
  475. name_len = EXT2_DIR_REC_LEN(de->name_len);
  476. rec_len = ext2_rec_len_from_disk(de->rec_len);
  477. if (!de->inode && rec_len >= reclen)
  478. goto got_it;
  479. if (rec_len >= name_len + reclen)
  480. goto got_it;
  481. de = (ext2_dirent *) ((char *) de + rec_len);
  482. }
  483. folio_unlock(folio);
  484. folio_release_kmap(folio, kaddr);
  485. }
  486. BUG();
  487. return -EINVAL;
  488. got_it:
  489. pos = folio_pos(folio) + offset_in_folio(folio, de);
  490. err = ext2_prepare_chunk(folio, pos, rec_len);
  491. if (err)
  492. goto out_unlock;
  493. if (de->inode) {
  494. ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
  495. de1->rec_len = ext2_rec_len_to_disk(rec_len - name_len);
  496. de->rec_len = ext2_rec_len_to_disk(name_len);
  497. de = de1;
  498. }
  499. de->name_len = namelen;
  500. memcpy(de->name, name, namelen);
  501. de->inode = cpu_to_le32(inode->i_ino);
  502. ext2_set_de_type (de, inode);
  503. ext2_commit_chunk(folio, pos, rec_len);
  504. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  505. EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
  506. mark_inode_dirty(dir);
  507. err = ext2_handle_dirsync(dir);
  508. /* OFFSET_CACHE */
  509. out_put:
  510. folio_release_kmap(folio, de);
  511. return err;
  512. out_unlock:
  513. folio_unlock(folio);
  514. goto out_put;
  515. }
  516. /*
  517. * ext2_delete_entry deletes a directory entry by merging it with the
  518. * previous entry. Page is up-to-date.
  519. */
  520. int ext2_delete_entry(struct ext2_dir_entry_2 *dir, struct folio *folio)
  521. {
  522. struct inode *inode = folio->mapping->host;
  523. size_t from, to;
  524. char *kaddr;
  525. loff_t pos;
  526. ext2_dirent *de, *pde = NULL;
  527. int err;
  528. from = offset_in_folio(folio, dir);
  529. to = from + ext2_rec_len_from_disk(dir->rec_len);
  530. kaddr = (char *)dir - from;
  531. from &= ~(ext2_chunk_size(inode)-1);
  532. de = (ext2_dirent *)(kaddr + from);
  533. while ((char*)de < (char*)dir) {
  534. if (de->rec_len == 0) {
  535. ext2_error(inode->i_sb, __func__,
  536. "zero-length directory entry");
  537. return -EIO;
  538. }
  539. pde = de;
  540. de = ext2_next_entry(de);
  541. }
  542. if (pde)
  543. from = offset_in_folio(folio, pde);
  544. pos = folio_pos(folio) + from;
  545. folio_lock(folio);
  546. err = ext2_prepare_chunk(folio, pos, to - from);
  547. if (err) {
  548. folio_unlock(folio);
  549. return err;
  550. }
  551. if (pde)
  552. pde->rec_len = ext2_rec_len_to_disk(to - from);
  553. dir->inode = 0;
  554. ext2_commit_chunk(folio, pos, to - from);
  555. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  556. EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL;
  557. mark_inode_dirty(inode);
  558. return ext2_handle_dirsync(inode);
  559. }
  560. /*
  561. * Set the first fragment of directory.
  562. */
  563. int ext2_make_empty(struct inode *inode, struct inode *parent)
  564. {
  565. struct folio *folio = filemap_grab_folio(inode->i_mapping, 0);
  566. unsigned chunk_size = ext2_chunk_size(inode);
  567. struct ext2_dir_entry_2 * de;
  568. int err;
  569. void *kaddr;
  570. if (IS_ERR(folio))
  571. return PTR_ERR(folio);
  572. err = ext2_prepare_chunk(folio, 0, chunk_size);
  573. if (err) {
  574. folio_unlock(folio);
  575. goto fail;
  576. }
  577. kaddr = kmap_local_folio(folio, 0);
  578. memset(kaddr, 0, chunk_size);
  579. de = (struct ext2_dir_entry_2 *)kaddr;
  580. de->name_len = 1;
  581. de->rec_len = ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1));
  582. memcpy (de->name, ".\0\0", 4);
  583. de->inode = cpu_to_le32(inode->i_ino);
  584. ext2_set_de_type (de, inode);
  585. de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1));
  586. de->name_len = 2;
  587. de->rec_len = ext2_rec_len_to_disk(chunk_size - EXT2_DIR_REC_LEN(1));
  588. de->inode = cpu_to_le32(parent->i_ino);
  589. memcpy (de->name, "..\0", 4);
  590. ext2_set_de_type (de, inode);
  591. kunmap_local(kaddr);
  592. ext2_commit_chunk(folio, 0, chunk_size);
  593. err = ext2_handle_dirsync(inode);
  594. fail:
  595. folio_put(folio);
  596. return err;
  597. }
  598. /*
  599. * routine to check that the specified directory is empty (for rmdir)
  600. */
  601. int ext2_empty_dir(struct inode *inode)
  602. {
  603. struct folio *folio;
  604. char *kaddr;
  605. unsigned long i, npages = dir_pages(inode);
  606. for (i = 0; i < npages; i++) {
  607. ext2_dirent *de;
  608. kaddr = ext2_get_folio(inode, i, 0, &folio);
  609. if (IS_ERR(kaddr))
  610. return 0;
  611. de = (ext2_dirent *)kaddr;
  612. kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1);
  613. while ((char *)de <= kaddr) {
  614. if (de->rec_len == 0) {
  615. ext2_error(inode->i_sb, __func__,
  616. "zero-length directory entry");
  617. printk("kaddr=%p, de=%p\n", kaddr, de);
  618. goto not_empty;
  619. }
  620. if (de->inode != 0) {
  621. /* check for . and .. */
  622. if (de->name[0] != '.')
  623. goto not_empty;
  624. if (de->name_len > 2)
  625. goto not_empty;
  626. if (de->name_len < 2) {
  627. if (de->inode !=
  628. cpu_to_le32(inode->i_ino))
  629. goto not_empty;
  630. } else if (de->name[1] != '.')
  631. goto not_empty;
  632. }
  633. de = ext2_next_entry(de);
  634. }
  635. folio_release_kmap(folio, kaddr);
  636. }
  637. return 1;
  638. not_empty:
  639. folio_release_kmap(folio, kaddr);
  640. return 0;
  641. }
  642. static int ext2_dir_open(struct inode *inode, struct file *file)
  643. {
  644. file->private_data = kzalloc(sizeof(u64), GFP_KERNEL);
  645. if (!file->private_data)
  646. return -ENOMEM;
  647. return 0;
  648. }
  649. static int ext2_dir_release(struct inode *inode, struct file *file)
  650. {
  651. kfree(file->private_data);
  652. return 0;
  653. }
  654. static loff_t ext2_dir_llseek(struct file *file, loff_t offset, int whence)
  655. {
  656. return generic_llseek_cookie(file, offset, whence,
  657. (u64 *)file->private_data);
  658. }
  659. const struct file_operations ext2_dir_operations = {
  660. .open = ext2_dir_open,
  661. .release = ext2_dir_release,
  662. .llseek = ext2_dir_llseek,
  663. .read = generic_read_dir,
  664. .iterate_shared = ext2_readdir,
  665. .unlocked_ioctl = ext2_ioctl,
  666. #ifdef CONFIG_COMPAT
  667. .compat_ioctl = ext2_compat_ioctl,
  668. #endif
  669. .fsync = ext2_fsync,
  670. };