dir.c 18 KB

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