dir.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * linux/fs/ufs/ufs_dir.c
  3. *
  4. * Copyright (C) 1996
  5. * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
  6. * Laboratory for Computer Science Research Computing Facility
  7. * Rutgers, The State University of New Jersey
  8. *
  9. * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406
  10. *
  11. * 4.4BSD (FreeBSD) support added on February 1st 1998 by
  12. * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
  13. * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
  14. *
  15. * Migration to usage of "page cache" on May 2006 by
  16. * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
  17. */
  18. #include <linux/time.h>
  19. #include <linux/fs.h>
  20. #include <linux/swap.h>
  21. #include "ufs_fs.h"
  22. #include "ufs.h"
  23. #include "swab.h"
  24. #include "util.h"
  25. /*
  26. * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
  27. *
  28. * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller.
  29. */
  30. static inline int ufs_match(struct super_block *sb, int len,
  31. const unsigned char *name, struct ufs_dir_entry *de)
  32. {
  33. if (len != ufs_get_de_namlen(sb, de))
  34. return 0;
  35. if (!de->d_ino)
  36. return 0;
  37. return !memcmp(name, de->d_name, len);
  38. }
  39. static int ufs_commit_chunk(struct page *page, loff_t pos, unsigned len)
  40. {
  41. struct address_space *mapping = page->mapping;
  42. struct inode *dir = mapping->host;
  43. int err = 0;
  44. dir->i_version++;
  45. block_write_end(NULL, mapping, pos, len, len, page, NULL);
  46. if (pos+len > dir->i_size) {
  47. i_size_write(dir, pos+len);
  48. mark_inode_dirty(dir);
  49. }
  50. if (IS_DIRSYNC(dir))
  51. err = write_one_page(page, 1);
  52. else
  53. unlock_page(page);
  54. return err;
  55. }
  56. static inline void ufs_put_page(struct page *page)
  57. {
  58. kunmap(page);
  59. page_cache_release(page);
  60. }
  61. static inline unsigned long ufs_dir_pages(struct inode *inode)
  62. {
  63. return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
  64. }
  65. ino_t ufs_inode_by_name(struct inode *dir, const struct qstr *qstr)
  66. {
  67. ino_t res = 0;
  68. struct ufs_dir_entry *de;
  69. struct page *page;
  70. de = ufs_find_entry(dir, qstr, &page);
  71. if (de) {
  72. res = fs32_to_cpu(dir->i_sb, de->d_ino);
  73. ufs_put_page(page);
  74. }
  75. return res;
  76. }
  77. /* Releases the page */
  78. void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
  79. struct page *page, struct inode *inode)
  80. {
  81. loff_t pos = page_offset(page) +
  82. (char *) de - (char *) page_address(page);
  83. unsigned len = fs16_to_cpu(dir->i_sb, de->d_reclen);
  84. int err;
  85. lock_page(page);
  86. err = ufs_prepare_chunk(page, pos, len);
  87. BUG_ON(err);
  88. de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino);
  89. ufs_set_de_type(dir->i_sb, de, inode->i_mode);
  90. err = ufs_commit_chunk(page, pos, len);
  91. ufs_put_page(page);
  92. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  93. mark_inode_dirty(dir);
  94. }
  95. static void ufs_check_page(struct page *page)
  96. {
  97. struct inode *dir = page->mapping->host;
  98. struct super_block *sb = dir->i_sb;
  99. char *kaddr = page_address(page);
  100. unsigned offs, rec_len;
  101. unsigned limit = PAGE_CACHE_SIZE;
  102. const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1;
  103. struct ufs_dir_entry *p;
  104. char *error;
  105. if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
  106. limit = dir->i_size & ~PAGE_CACHE_MASK;
  107. if (limit & chunk_mask)
  108. goto Ebadsize;
  109. if (!limit)
  110. goto out;
  111. }
  112. for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) {
  113. p = (struct ufs_dir_entry *)(kaddr + offs);
  114. rec_len = fs16_to_cpu(sb, p->d_reclen);
  115. if (rec_len < UFS_DIR_REC_LEN(1))
  116. goto Eshort;
  117. if (rec_len & 3)
  118. goto Ealign;
  119. if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p)))
  120. goto Enamelen;
  121. if (((offs + rec_len - 1) ^ offs) & ~chunk_mask)
  122. goto Espan;
  123. if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg *
  124. UFS_SB(sb)->s_uspi->s_ncg))
  125. goto Einumber;
  126. }
  127. if (offs != limit)
  128. goto Eend;
  129. out:
  130. SetPageChecked(page);
  131. return;
  132. /* Too bad, we had an error */
  133. Ebadsize:
  134. ufs_error(sb, "ufs_check_page",
  135. "size of directory #%lu is not a multiple of chunk size",
  136. dir->i_ino
  137. );
  138. goto fail;
  139. Eshort:
  140. error = "rec_len is smaller than minimal";
  141. goto bad_entry;
  142. Ealign:
  143. error = "unaligned directory entry";
  144. goto bad_entry;
  145. Enamelen:
  146. error = "rec_len is too small for name_len";
  147. goto bad_entry;
  148. Espan:
  149. error = "directory entry across blocks";
  150. goto bad_entry;
  151. Einumber:
  152. error = "inode out of bounds";
  153. bad_entry:
  154. ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - "
  155. "offset=%lu, rec_len=%d, name_len=%d",
  156. dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
  157. rec_len, ufs_get_de_namlen(sb, p));
  158. goto fail;
  159. Eend:
  160. p = (struct ufs_dir_entry *)(kaddr + offs);
  161. ufs_error(sb, __func__,
  162. "entry in directory #%lu spans the page boundary"
  163. "offset=%lu",
  164. dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs);
  165. fail:
  166. SetPageChecked(page);
  167. SetPageError(page);
  168. }
  169. static struct page *ufs_get_page(struct inode *dir, unsigned long n)
  170. {
  171. struct address_space *mapping = dir->i_mapping;
  172. struct page *page = read_mapping_page(mapping, n, NULL);
  173. if (!IS_ERR(page)) {
  174. kmap(page);
  175. if (!PageChecked(page))
  176. ufs_check_page(page);
  177. if (PageError(page))
  178. goto fail;
  179. }
  180. return page;
  181. fail:
  182. ufs_put_page(page);
  183. return ERR_PTR(-EIO);
  184. }
  185. /*
  186. * Return the offset into page `page_nr' of the last valid
  187. * byte in that page, plus one.
  188. */
  189. static unsigned
  190. ufs_last_byte(struct inode *inode, unsigned long page_nr)
  191. {
  192. unsigned last_byte = inode->i_size;
  193. last_byte -= page_nr << PAGE_CACHE_SHIFT;
  194. if (last_byte > PAGE_CACHE_SIZE)
  195. last_byte = PAGE_CACHE_SIZE;
  196. return last_byte;
  197. }
  198. static inline struct ufs_dir_entry *
  199. ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p)
  200. {
  201. return (struct ufs_dir_entry *)((char *)p +
  202. fs16_to_cpu(sb, p->d_reclen));
  203. }
  204. struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p)
  205. {
  206. struct page *page = ufs_get_page(dir, 0);
  207. struct ufs_dir_entry *de = NULL;
  208. if (!IS_ERR(page)) {
  209. de = ufs_next_entry(dir->i_sb,
  210. (struct ufs_dir_entry *)page_address(page));
  211. *p = page;
  212. }
  213. return de;
  214. }
  215. /*
  216. * ufs_find_entry()
  217. *
  218. * finds an entry in the specified directory with the wanted name. It
  219. * returns the page in which the entry was found, and the entry itself
  220. * (as a parameter - res_dir). Page is returned mapped and unlocked.
  221. * Entry is guaranteed to be valid.
  222. */
  223. struct ufs_dir_entry *ufs_find_entry(struct inode *dir, const struct qstr *qstr,
  224. struct page **res_page)
  225. {
  226. struct super_block *sb = dir->i_sb;
  227. const unsigned char *name = qstr->name;
  228. int namelen = qstr->len;
  229. unsigned reclen = UFS_DIR_REC_LEN(namelen);
  230. unsigned long start, n;
  231. unsigned long npages = ufs_dir_pages(dir);
  232. struct page *page = NULL;
  233. struct ufs_inode_info *ui = UFS_I(dir);
  234. struct ufs_dir_entry *de;
  235. UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen);
  236. if (npages == 0 || namelen > UFS_MAXNAMLEN)
  237. goto out;
  238. /* OFFSET_CACHE */
  239. *res_page = NULL;
  240. start = ui->i_dir_start_lookup;
  241. if (start >= npages)
  242. start = 0;
  243. n = start;
  244. do {
  245. char *kaddr;
  246. page = ufs_get_page(dir, n);
  247. if (!IS_ERR(page)) {
  248. kaddr = page_address(page);
  249. de = (struct ufs_dir_entry *) kaddr;
  250. kaddr += ufs_last_byte(dir, n) - reclen;
  251. while ((char *) de <= kaddr) {
  252. if (de->d_reclen == 0) {
  253. ufs_error(dir->i_sb, __func__,
  254. "zero-length directory entry");
  255. ufs_put_page(page);
  256. goto out;
  257. }
  258. if (ufs_match(sb, namelen, name, de))
  259. goto found;
  260. de = ufs_next_entry(sb, de);
  261. }
  262. ufs_put_page(page);
  263. }
  264. if (++n >= npages)
  265. n = 0;
  266. } while (n != start);
  267. out:
  268. return NULL;
  269. found:
  270. *res_page = page;
  271. ui->i_dir_start_lookup = n;
  272. return de;
  273. }
  274. /*
  275. * Parent is locked.
  276. */
  277. int ufs_add_link(struct dentry *dentry, struct inode *inode)
  278. {
  279. struct inode *dir = dentry->d_parent->d_inode;
  280. const unsigned char *name = dentry->d_name.name;
  281. int namelen = dentry->d_name.len;
  282. struct super_block *sb = dir->i_sb;
  283. unsigned reclen = UFS_DIR_REC_LEN(namelen);
  284. const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
  285. unsigned short rec_len, name_len;
  286. struct page *page = NULL;
  287. struct ufs_dir_entry *de;
  288. unsigned long npages = ufs_dir_pages(dir);
  289. unsigned long n;
  290. char *kaddr;
  291. loff_t pos;
  292. int err;
  293. UFSD("ENTER, name %s, namelen %u\n", name, namelen);
  294. /*
  295. * We take care of directory expansion in the same loop.
  296. * This code plays outside i_size, so it locks the page
  297. * to protect that region.
  298. */
  299. for (n = 0; n <= npages; n++) {
  300. char *dir_end;
  301. page = ufs_get_page(dir, n);
  302. err = PTR_ERR(page);
  303. if (IS_ERR(page))
  304. goto out;
  305. lock_page(page);
  306. kaddr = page_address(page);
  307. dir_end = kaddr + ufs_last_byte(dir, n);
  308. de = (struct ufs_dir_entry *)kaddr;
  309. kaddr += PAGE_CACHE_SIZE - reclen;
  310. while ((char *)de <= kaddr) {
  311. if ((char *)de == dir_end) {
  312. /* We hit i_size */
  313. name_len = 0;
  314. rec_len = chunk_size;
  315. de->d_reclen = cpu_to_fs16(sb, chunk_size);
  316. de->d_ino = 0;
  317. goto got_it;
  318. }
  319. if (de->d_reclen == 0) {
  320. ufs_error(dir->i_sb, __func__,
  321. "zero-length directory entry");
  322. err = -EIO;
  323. goto out_unlock;
  324. }
  325. err = -EEXIST;
  326. if (ufs_match(sb, namelen, name, de))
  327. goto out_unlock;
  328. name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de));
  329. rec_len = fs16_to_cpu(sb, de->d_reclen);
  330. if (!de->d_ino && rec_len >= reclen)
  331. goto got_it;
  332. if (rec_len >= name_len + reclen)
  333. goto got_it;
  334. de = (struct ufs_dir_entry *) ((char *) de + rec_len);
  335. }
  336. unlock_page(page);
  337. ufs_put_page(page);
  338. }
  339. BUG();
  340. return -EINVAL;
  341. got_it:
  342. pos = page_offset(page) +
  343. (char*)de - (char*)page_address(page);
  344. err = ufs_prepare_chunk(page, pos, rec_len);
  345. if (err)
  346. goto out_unlock;
  347. if (de->d_ino) {
  348. struct ufs_dir_entry *de1 =
  349. (struct ufs_dir_entry *) ((char *) de + name_len);
  350. de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len);
  351. de->d_reclen = cpu_to_fs16(sb, name_len);
  352. de = de1;
  353. }
  354. ufs_set_de_namlen(sb, de, namelen);
  355. memcpy(de->d_name, name, namelen + 1);
  356. de->d_ino = cpu_to_fs32(sb, inode->i_ino);
  357. ufs_set_de_type(sb, de, inode->i_mode);
  358. err = ufs_commit_chunk(page, pos, rec_len);
  359. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  360. mark_inode_dirty(dir);
  361. /* OFFSET_CACHE */
  362. out_put:
  363. ufs_put_page(page);
  364. out:
  365. return err;
  366. out_unlock:
  367. unlock_page(page);
  368. goto out_put;
  369. }
  370. static inline unsigned
  371. ufs_validate_entry(struct super_block *sb, char *base,
  372. unsigned offset, unsigned mask)
  373. {
  374. struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset);
  375. struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask));
  376. while ((char*)p < (char*)de) {
  377. if (p->d_reclen == 0)
  378. break;
  379. p = ufs_next_entry(sb, p);
  380. }
  381. return (char *)p - base;
  382. }
  383. /*
  384. * This is blatantly stolen from ext2fs
  385. */
  386. static int
  387. ufs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  388. {
  389. loff_t pos = filp->f_pos;
  390. struct inode *inode = filp->f_path.dentry->d_inode;
  391. struct super_block *sb = inode->i_sb;
  392. unsigned int offset = pos & ~PAGE_CACHE_MASK;
  393. unsigned long n = pos >> PAGE_CACHE_SHIFT;
  394. unsigned long npages = ufs_dir_pages(inode);
  395. unsigned chunk_mask = ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
  396. int need_revalidate = filp->f_version != inode->i_version;
  397. unsigned flags = UFS_SB(sb)->s_flags;
  398. UFSD("BEGIN\n");
  399. if (pos > inode->i_size - UFS_DIR_REC_LEN(1))
  400. return 0;
  401. for ( ; n < npages; n++, offset = 0) {
  402. char *kaddr, *limit;
  403. struct ufs_dir_entry *de;
  404. struct page *page = ufs_get_page(inode, n);
  405. if (IS_ERR(page)) {
  406. ufs_error(sb, __func__,
  407. "bad page in #%lu",
  408. inode->i_ino);
  409. filp->f_pos += PAGE_CACHE_SIZE - offset;
  410. return -EIO;
  411. }
  412. kaddr = page_address(page);
  413. if (unlikely(need_revalidate)) {
  414. if (offset) {
  415. offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask);
  416. filp->f_pos = (n<<PAGE_CACHE_SHIFT) + offset;
  417. }
  418. filp->f_version = inode->i_version;
  419. need_revalidate = 0;
  420. }
  421. de = (struct ufs_dir_entry *)(kaddr+offset);
  422. limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1);
  423. for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) {
  424. if (de->d_reclen == 0) {
  425. ufs_error(sb, __func__,
  426. "zero-length directory entry");
  427. ufs_put_page(page);
  428. return -EIO;
  429. }
  430. if (de->d_ino) {
  431. int over;
  432. unsigned char d_type = DT_UNKNOWN;
  433. offset = (char *)de - kaddr;
  434. UFSD("filldir(%s,%u)\n", de->d_name,
  435. fs32_to_cpu(sb, de->d_ino));
  436. UFSD("namlen %u\n", ufs_get_de_namlen(sb, de));
  437. if ((flags & UFS_DE_MASK) == UFS_DE_44BSD)
  438. d_type = de->d_u.d_44.d_type;
  439. over = filldir(dirent, de->d_name,
  440. ufs_get_de_namlen(sb, de),
  441. (n<<PAGE_CACHE_SHIFT) | offset,
  442. fs32_to_cpu(sb, de->d_ino), d_type);
  443. if (over) {
  444. ufs_put_page(page);
  445. return 0;
  446. }
  447. }
  448. filp->f_pos += fs16_to_cpu(sb, de->d_reclen);
  449. }
  450. ufs_put_page(page);
  451. }
  452. return 0;
  453. }
  454. /*
  455. * ufs_delete_entry deletes a directory entry by merging it with the
  456. * previous entry.
  457. */
  458. int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir,
  459. struct page * page)
  460. {
  461. struct super_block *sb = inode->i_sb;
  462. char *kaddr = page_address(page);
  463. unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
  464. unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen);
  465. loff_t pos;
  466. struct ufs_dir_entry *pde = NULL;
  467. struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from);
  468. int err;
  469. UFSD("ENTER\n");
  470. UFSD("ino %u, reclen %u, namlen %u, name %s\n",
  471. fs32_to_cpu(sb, de->d_ino),
  472. fs16_to_cpu(sb, de->d_reclen),
  473. ufs_get_de_namlen(sb, de), de->d_name);
  474. while ((char*)de < (char*)dir) {
  475. if (de->d_reclen == 0) {
  476. ufs_error(inode->i_sb, __func__,
  477. "zero-length directory entry");
  478. err = -EIO;
  479. goto out;
  480. }
  481. pde = de;
  482. de = ufs_next_entry(sb, de);
  483. }
  484. if (pde)
  485. from = (char*)pde - (char*)page_address(page);
  486. pos = page_offset(page) + from;
  487. lock_page(page);
  488. err = ufs_prepare_chunk(page, pos, to - from);
  489. BUG_ON(err);
  490. if (pde)
  491. pde->d_reclen = cpu_to_fs16(sb, to - from);
  492. dir->d_ino = 0;
  493. err = ufs_commit_chunk(page, pos, to - from);
  494. inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
  495. mark_inode_dirty(inode);
  496. out:
  497. ufs_put_page(page);
  498. UFSD("EXIT\n");
  499. return err;
  500. }
  501. int ufs_make_empty(struct inode * inode, struct inode *dir)
  502. {
  503. struct super_block * sb = dir->i_sb;
  504. struct address_space *mapping = inode->i_mapping;
  505. struct page *page = grab_cache_page(mapping, 0);
  506. const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
  507. struct ufs_dir_entry * de;
  508. char *base;
  509. int err;
  510. if (!page)
  511. return -ENOMEM;
  512. err = ufs_prepare_chunk(page, 0, chunk_size);
  513. if (err) {
  514. unlock_page(page);
  515. goto fail;
  516. }
  517. kmap(page);
  518. base = (char*)page_address(page);
  519. memset(base, 0, PAGE_CACHE_SIZE);
  520. de = (struct ufs_dir_entry *) base;
  521. de->d_ino = cpu_to_fs32(sb, inode->i_ino);
  522. ufs_set_de_type(sb, de, inode->i_mode);
  523. ufs_set_de_namlen(sb, de, 1);
  524. de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1));
  525. strcpy (de->d_name, ".");
  526. de = (struct ufs_dir_entry *)
  527. ((char *)de + fs16_to_cpu(sb, de->d_reclen));
  528. de->d_ino = cpu_to_fs32(sb, dir->i_ino);
  529. ufs_set_de_type(sb, de, dir->i_mode);
  530. de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1));
  531. ufs_set_de_namlen(sb, de, 2);
  532. strcpy (de->d_name, "..");
  533. kunmap(page);
  534. err = ufs_commit_chunk(page, 0, chunk_size);
  535. fail:
  536. page_cache_release(page);
  537. return err;
  538. }
  539. /*
  540. * routine to check that the specified directory is empty (for rmdir)
  541. */
  542. int ufs_empty_dir(struct inode * inode)
  543. {
  544. struct super_block *sb = inode->i_sb;
  545. struct page *page = NULL;
  546. unsigned long i, npages = ufs_dir_pages(inode);
  547. for (i = 0; i < npages; i++) {
  548. char *kaddr;
  549. struct ufs_dir_entry *de;
  550. page = ufs_get_page(inode, i);
  551. if (IS_ERR(page))
  552. continue;
  553. kaddr = page_address(page);
  554. de = (struct ufs_dir_entry *)kaddr;
  555. kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1);
  556. while ((char *)de <= kaddr) {
  557. if (de->d_reclen == 0) {
  558. ufs_error(inode->i_sb, __func__,
  559. "zero-length directory entry: "
  560. "kaddr=%p, de=%p\n", kaddr, de);
  561. goto not_empty;
  562. }
  563. if (de->d_ino) {
  564. u16 namelen=ufs_get_de_namlen(sb, de);
  565. /* check for . and .. */
  566. if (de->d_name[0] != '.')
  567. goto not_empty;
  568. if (namelen > 2)
  569. goto not_empty;
  570. if (namelen < 2) {
  571. if (inode->i_ino !=
  572. fs32_to_cpu(sb, de->d_ino))
  573. goto not_empty;
  574. } else if (de->d_name[1] != '.')
  575. goto not_empty;
  576. }
  577. de = ufs_next_entry(sb, de);
  578. }
  579. ufs_put_page(page);
  580. }
  581. return 1;
  582. not_empty:
  583. ufs_put_page(page);
  584. return 0;
  585. }
  586. const struct file_operations ufs_dir_operations = {
  587. .read = generic_read_dir,
  588. .readdir = ufs_readdir,
  589. .fsync = generic_file_fsync,
  590. .llseek = generic_file_llseek,
  591. };