dir.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * Copyright (C) 2005, 2006
  3. * Avishay Traeger (avishay@gmail.com)
  4. * Copyright (C) 2008, 2009
  5. * Boaz Harrosh <ooo@electrozaur.com>
  6. *
  7. * Copyrights for code taken from ext2:
  8. * Copyright (C) 1992, 1993, 1994, 1995
  9. * Remy Card (card@masi.ibp.fr)
  10. * Laboratoire MASI - Institut Blaise Pascal
  11. * Universite Pierre et Marie Curie (Paris VI)
  12. * from
  13. * linux/fs/minix/inode.c
  14. * Copyright (C) 1991, 1992 Linus Torvalds
  15. *
  16. * This file is part of exofs.
  17. *
  18. * exofs is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation. Since it is based on ext2, and the only
  21. * valid version of GPL for the Linux kernel is version 2, the only valid
  22. * version of GPL for exofs is version 2.
  23. *
  24. * exofs is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with exofs; if not, write to the Free Software
  31. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  32. */
  33. #include "exofs.h"
  34. static inline unsigned exofs_chunk_size(struct inode *inode)
  35. {
  36. return inode->i_sb->s_blocksize;
  37. }
  38. static inline void exofs_put_page(struct page *page)
  39. {
  40. kunmap(page);
  41. put_page(page);
  42. }
  43. static unsigned exofs_last_byte(struct inode *inode, unsigned long page_nr)
  44. {
  45. loff_t last_byte = inode->i_size;
  46. last_byte -= page_nr << PAGE_SHIFT;
  47. if (last_byte > PAGE_SIZE)
  48. last_byte = PAGE_SIZE;
  49. return last_byte;
  50. }
  51. static int exofs_commit_chunk(struct page *page, loff_t pos, unsigned len)
  52. {
  53. struct address_space *mapping = page->mapping;
  54. struct inode *dir = mapping->host;
  55. int err = 0;
  56. dir->i_version++;
  57. if (!PageUptodate(page))
  58. SetPageUptodate(page);
  59. if (pos+len > dir->i_size) {
  60. i_size_write(dir, pos+len);
  61. mark_inode_dirty(dir);
  62. }
  63. set_page_dirty(page);
  64. if (IS_DIRSYNC(dir))
  65. err = write_one_page(page, 1);
  66. else
  67. unlock_page(page);
  68. return err;
  69. }
  70. static bool exofs_check_page(struct page *page)
  71. {
  72. struct inode *dir = page->mapping->host;
  73. unsigned chunk_size = exofs_chunk_size(dir);
  74. char *kaddr = page_address(page);
  75. unsigned offs, rec_len;
  76. unsigned limit = PAGE_SIZE;
  77. struct exofs_dir_entry *p;
  78. char *error;
  79. /* if the page is the last one in the directory */
  80. if ((dir->i_size >> PAGE_SHIFT) == page->index) {
  81. limit = dir->i_size & ~PAGE_MASK;
  82. if (limit & (chunk_size - 1))
  83. goto Ebadsize;
  84. if (!limit)
  85. goto out;
  86. }
  87. for (offs = 0; offs <= limit - EXOFS_DIR_REC_LEN(1); offs += rec_len) {
  88. p = (struct exofs_dir_entry *)(kaddr + offs);
  89. rec_len = le16_to_cpu(p->rec_len);
  90. if (rec_len < EXOFS_DIR_REC_LEN(1))
  91. goto Eshort;
  92. if (rec_len & 3)
  93. goto Ealign;
  94. if (rec_len < EXOFS_DIR_REC_LEN(p->name_len))
  95. goto Enamelen;
  96. if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
  97. goto Espan;
  98. }
  99. if (offs != limit)
  100. goto Eend;
  101. out:
  102. SetPageChecked(page);
  103. return true;
  104. Ebadsize:
  105. EXOFS_ERR("ERROR [exofs_check_page]: "
  106. "size of directory(0x%lx) is not a multiple of chunk size\n",
  107. dir->i_ino
  108. );
  109. goto fail;
  110. Eshort:
  111. error = "rec_len is smaller than minimal";
  112. goto bad_entry;
  113. Ealign:
  114. error = "unaligned directory entry";
  115. goto bad_entry;
  116. Enamelen:
  117. error = "rec_len is too small for name_len";
  118. goto bad_entry;
  119. Espan:
  120. error = "directory entry across blocks";
  121. goto bad_entry;
  122. bad_entry:
  123. EXOFS_ERR(
  124. "ERROR [exofs_check_page]: bad entry in directory(0x%lx): %s - "
  125. "offset=%lu, inode=0x%llx, rec_len=%d, name_len=%d\n",
  126. dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs,
  127. _LLU(le64_to_cpu(p->inode_no)),
  128. rec_len, p->name_len);
  129. goto fail;
  130. Eend:
  131. p = (struct exofs_dir_entry *)(kaddr + offs);
  132. EXOFS_ERR("ERROR [exofs_check_page]: "
  133. "entry in directory(0x%lx) spans the page boundary"
  134. "offset=%lu, inode=0x%llx\n",
  135. dir->i_ino, (page->index<<PAGE_SHIFT)+offs,
  136. _LLU(le64_to_cpu(p->inode_no)));
  137. fail:
  138. SetPageError(page);
  139. return false;
  140. }
  141. static struct page *exofs_get_page(struct inode *dir, unsigned long n)
  142. {
  143. struct address_space *mapping = dir->i_mapping;
  144. struct page *page = read_mapping_page(mapping, n, NULL);
  145. if (!IS_ERR(page)) {
  146. kmap(page);
  147. if (unlikely(!PageChecked(page))) {
  148. if (PageError(page) || !exofs_check_page(page))
  149. goto fail;
  150. }
  151. }
  152. return page;
  153. fail:
  154. exofs_put_page(page);
  155. return ERR_PTR(-EIO);
  156. }
  157. static inline int exofs_match(int len, const unsigned char *name,
  158. struct exofs_dir_entry *de)
  159. {
  160. if (len != de->name_len)
  161. return 0;
  162. if (!de->inode_no)
  163. return 0;
  164. return !memcmp(name, de->name, len);
  165. }
  166. static inline
  167. struct exofs_dir_entry *exofs_next_entry(struct exofs_dir_entry *p)
  168. {
  169. return (struct exofs_dir_entry *)((char *)p + le16_to_cpu(p->rec_len));
  170. }
  171. static inline unsigned
  172. exofs_validate_entry(char *base, unsigned offset, unsigned mask)
  173. {
  174. struct exofs_dir_entry *de = (struct exofs_dir_entry *)(base + offset);
  175. struct exofs_dir_entry *p =
  176. (struct exofs_dir_entry *)(base + (offset&mask));
  177. while ((char *)p < (char *)de) {
  178. if (p->rec_len == 0)
  179. break;
  180. p = exofs_next_entry(p);
  181. }
  182. return (char *)p - base;
  183. }
  184. static unsigned char exofs_filetype_table[EXOFS_FT_MAX] = {
  185. [EXOFS_FT_UNKNOWN] = DT_UNKNOWN,
  186. [EXOFS_FT_REG_FILE] = DT_REG,
  187. [EXOFS_FT_DIR] = DT_DIR,
  188. [EXOFS_FT_CHRDEV] = DT_CHR,
  189. [EXOFS_FT_BLKDEV] = DT_BLK,
  190. [EXOFS_FT_FIFO] = DT_FIFO,
  191. [EXOFS_FT_SOCK] = DT_SOCK,
  192. [EXOFS_FT_SYMLINK] = DT_LNK,
  193. };
  194. #define S_SHIFT 12
  195. static unsigned char exofs_type_by_mode[S_IFMT >> S_SHIFT] = {
  196. [S_IFREG >> S_SHIFT] = EXOFS_FT_REG_FILE,
  197. [S_IFDIR >> S_SHIFT] = EXOFS_FT_DIR,
  198. [S_IFCHR >> S_SHIFT] = EXOFS_FT_CHRDEV,
  199. [S_IFBLK >> S_SHIFT] = EXOFS_FT_BLKDEV,
  200. [S_IFIFO >> S_SHIFT] = EXOFS_FT_FIFO,
  201. [S_IFSOCK >> S_SHIFT] = EXOFS_FT_SOCK,
  202. [S_IFLNK >> S_SHIFT] = EXOFS_FT_SYMLINK,
  203. };
  204. static inline
  205. void exofs_set_de_type(struct exofs_dir_entry *de, struct inode *inode)
  206. {
  207. umode_t mode = inode->i_mode;
  208. de->file_type = exofs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
  209. }
  210. static int
  211. exofs_readdir(struct file *file, struct dir_context *ctx)
  212. {
  213. loff_t pos = ctx->pos;
  214. struct inode *inode = file_inode(file);
  215. unsigned int offset = pos & ~PAGE_MASK;
  216. unsigned long n = pos >> PAGE_SHIFT;
  217. unsigned long npages = dir_pages(inode);
  218. unsigned chunk_mask = ~(exofs_chunk_size(inode)-1);
  219. int need_revalidate = (file->f_version != inode->i_version);
  220. if (pos > inode->i_size - EXOFS_DIR_REC_LEN(1))
  221. return 0;
  222. for ( ; n < npages; n++, offset = 0) {
  223. char *kaddr, *limit;
  224. struct exofs_dir_entry *de;
  225. struct page *page = exofs_get_page(inode, n);
  226. if (IS_ERR(page)) {
  227. EXOFS_ERR("ERROR: bad page in directory(0x%lx)\n",
  228. inode->i_ino);
  229. ctx->pos += PAGE_SIZE - offset;
  230. return PTR_ERR(page);
  231. }
  232. kaddr = page_address(page);
  233. if (unlikely(need_revalidate)) {
  234. if (offset) {
  235. offset = exofs_validate_entry(kaddr, offset,
  236. chunk_mask);
  237. ctx->pos = (n<<PAGE_SHIFT) + offset;
  238. }
  239. file->f_version = inode->i_version;
  240. need_revalidate = 0;
  241. }
  242. de = (struct exofs_dir_entry *)(kaddr + offset);
  243. limit = kaddr + exofs_last_byte(inode, n) -
  244. EXOFS_DIR_REC_LEN(1);
  245. for (; (char *)de <= limit; de = exofs_next_entry(de)) {
  246. if (de->rec_len == 0) {
  247. EXOFS_ERR("ERROR: "
  248. "zero-length entry in directory(0x%lx)\n",
  249. inode->i_ino);
  250. exofs_put_page(page);
  251. return -EIO;
  252. }
  253. if (de->inode_no) {
  254. unsigned char t;
  255. if (de->file_type < EXOFS_FT_MAX)
  256. t = exofs_filetype_table[de->file_type];
  257. else
  258. t = DT_UNKNOWN;
  259. if (!dir_emit(ctx, de->name, de->name_len,
  260. le64_to_cpu(de->inode_no),
  261. t)) {
  262. exofs_put_page(page);
  263. return 0;
  264. }
  265. }
  266. ctx->pos += le16_to_cpu(de->rec_len);
  267. }
  268. exofs_put_page(page);
  269. }
  270. return 0;
  271. }
  272. struct exofs_dir_entry *exofs_find_entry(struct inode *dir,
  273. struct dentry *dentry, struct page **res_page)
  274. {
  275. const unsigned char *name = dentry->d_name.name;
  276. int namelen = dentry->d_name.len;
  277. unsigned reclen = EXOFS_DIR_REC_LEN(namelen);
  278. unsigned long start, n;
  279. unsigned long npages = dir_pages(dir);
  280. struct page *page = NULL;
  281. struct exofs_i_info *oi = exofs_i(dir);
  282. struct exofs_dir_entry *de;
  283. if (npages == 0)
  284. goto out;
  285. *res_page = NULL;
  286. start = oi->i_dir_start_lookup;
  287. if (start >= npages)
  288. start = 0;
  289. n = start;
  290. do {
  291. char *kaddr;
  292. page = exofs_get_page(dir, n);
  293. if (!IS_ERR(page)) {
  294. kaddr = page_address(page);
  295. de = (struct exofs_dir_entry *) kaddr;
  296. kaddr += exofs_last_byte(dir, n) - reclen;
  297. while ((char *) de <= kaddr) {
  298. if (de->rec_len == 0) {
  299. EXOFS_ERR("ERROR: zero-length entry in "
  300. "directory(0x%lx)\n",
  301. dir->i_ino);
  302. exofs_put_page(page);
  303. goto out;
  304. }
  305. if (exofs_match(namelen, name, de))
  306. goto found;
  307. de = exofs_next_entry(de);
  308. }
  309. exofs_put_page(page);
  310. }
  311. if (++n >= npages)
  312. n = 0;
  313. } while (n != start);
  314. out:
  315. return NULL;
  316. found:
  317. *res_page = page;
  318. oi->i_dir_start_lookup = n;
  319. return de;
  320. }
  321. struct exofs_dir_entry *exofs_dotdot(struct inode *dir, struct page **p)
  322. {
  323. struct page *page = exofs_get_page(dir, 0);
  324. struct exofs_dir_entry *de = NULL;
  325. if (!IS_ERR(page)) {
  326. de = exofs_next_entry(
  327. (struct exofs_dir_entry *)page_address(page));
  328. *p = page;
  329. }
  330. return de;
  331. }
  332. ino_t exofs_parent_ino(struct dentry *child)
  333. {
  334. struct page *page;
  335. struct exofs_dir_entry *de;
  336. ino_t ino;
  337. de = exofs_dotdot(d_inode(child), &page);
  338. if (!de)
  339. return 0;
  340. ino = le64_to_cpu(de->inode_no);
  341. exofs_put_page(page);
  342. return ino;
  343. }
  344. ino_t exofs_inode_by_name(struct inode *dir, struct dentry *dentry)
  345. {
  346. ino_t res = 0;
  347. struct exofs_dir_entry *de;
  348. struct page *page;
  349. de = exofs_find_entry(dir, dentry, &page);
  350. if (de) {
  351. res = le64_to_cpu(de->inode_no);
  352. exofs_put_page(page);
  353. }
  354. return res;
  355. }
  356. int exofs_set_link(struct inode *dir, struct exofs_dir_entry *de,
  357. struct page *page, struct inode *inode)
  358. {
  359. loff_t pos = page_offset(page) +
  360. (char *) de - (char *) page_address(page);
  361. unsigned len = le16_to_cpu(de->rec_len);
  362. int err;
  363. lock_page(page);
  364. err = exofs_write_begin(NULL, page->mapping, pos, len,
  365. AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
  366. if (err)
  367. EXOFS_ERR("exofs_set_link: exofs_write_begin FAILED => %d\n",
  368. err);
  369. de->inode_no = cpu_to_le64(inode->i_ino);
  370. exofs_set_de_type(de, inode);
  371. if (likely(!err))
  372. err = exofs_commit_chunk(page, pos, len);
  373. exofs_put_page(page);
  374. dir->i_mtime = dir->i_ctime = current_time(dir);
  375. mark_inode_dirty(dir);
  376. return err;
  377. }
  378. int exofs_add_link(struct dentry *dentry, struct inode *inode)
  379. {
  380. struct inode *dir = d_inode(dentry->d_parent);
  381. const unsigned char *name = dentry->d_name.name;
  382. int namelen = dentry->d_name.len;
  383. unsigned chunk_size = exofs_chunk_size(dir);
  384. unsigned reclen = EXOFS_DIR_REC_LEN(namelen);
  385. unsigned short rec_len, name_len;
  386. struct page *page = NULL;
  387. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  388. struct exofs_dir_entry *de;
  389. unsigned long npages = dir_pages(dir);
  390. unsigned long n;
  391. char *kaddr;
  392. loff_t pos;
  393. int err;
  394. for (n = 0; n <= npages; n++) {
  395. char *dir_end;
  396. page = exofs_get_page(dir, n);
  397. err = PTR_ERR(page);
  398. if (IS_ERR(page))
  399. goto out;
  400. lock_page(page);
  401. kaddr = page_address(page);
  402. dir_end = kaddr + exofs_last_byte(dir, n);
  403. de = (struct exofs_dir_entry *)kaddr;
  404. kaddr += PAGE_SIZE - reclen;
  405. while ((char *)de <= kaddr) {
  406. if ((char *)de == dir_end) {
  407. name_len = 0;
  408. rec_len = chunk_size;
  409. de->rec_len = cpu_to_le16(chunk_size);
  410. de->inode_no = 0;
  411. goto got_it;
  412. }
  413. if (de->rec_len == 0) {
  414. EXOFS_ERR("ERROR: exofs_add_link: "
  415. "zero-length entry in directory(0x%lx)\n",
  416. inode->i_ino);
  417. err = -EIO;
  418. goto out_unlock;
  419. }
  420. err = -EEXIST;
  421. if (exofs_match(namelen, name, de))
  422. goto out_unlock;
  423. name_len = EXOFS_DIR_REC_LEN(de->name_len);
  424. rec_len = le16_to_cpu(de->rec_len);
  425. if (!de->inode_no && rec_len >= reclen)
  426. goto got_it;
  427. if (rec_len >= name_len + reclen)
  428. goto got_it;
  429. de = (struct exofs_dir_entry *) ((char *) de + rec_len);
  430. }
  431. unlock_page(page);
  432. exofs_put_page(page);
  433. }
  434. EXOFS_ERR("exofs_add_link: BAD dentry=%p or inode=0x%lx\n",
  435. dentry, inode->i_ino);
  436. return -EINVAL;
  437. got_it:
  438. pos = page_offset(page) +
  439. (char *)de - (char *)page_address(page);
  440. err = exofs_write_begin(NULL, page->mapping, pos, rec_len, 0,
  441. &page, NULL);
  442. if (err)
  443. goto out_unlock;
  444. if (de->inode_no) {
  445. struct exofs_dir_entry *de1 =
  446. (struct exofs_dir_entry *)((char *)de + name_len);
  447. de1->rec_len = cpu_to_le16(rec_len - name_len);
  448. de->rec_len = cpu_to_le16(name_len);
  449. de = de1;
  450. }
  451. de->name_len = namelen;
  452. memcpy(de->name, name, namelen);
  453. de->inode_no = cpu_to_le64(inode->i_ino);
  454. exofs_set_de_type(de, inode);
  455. err = exofs_commit_chunk(page, pos, rec_len);
  456. dir->i_mtime = dir->i_ctime = current_time(dir);
  457. mark_inode_dirty(dir);
  458. sbi->s_numfiles++;
  459. out_put:
  460. exofs_put_page(page);
  461. out:
  462. return err;
  463. out_unlock:
  464. unlock_page(page);
  465. goto out_put;
  466. }
  467. int exofs_delete_entry(struct exofs_dir_entry *dir, struct page *page)
  468. {
  469. struct address_space *mapping = page->mapping;
  470. struct inode *inode = mapping->host;
  471. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  472. char *kaddr = page_address(page);
  473. unsigned from = ((char *)dir - kaddr) & ~(exofs_chunk_size(inode)-1);
  474. unsigned to = ((char *)dir - kaddr) + le16_to_cpu(dir->rec_len);
  475. loff_t pos;
  476. struct exofs_dir_entry *pde = NULL;
  477. struct exofs_dir_entry *de = (struct exofs_dir_entry *) (kaddr + from);
  478. int err;
  479. while (de < dir) {
  480. if (de->rec_len == 0) {
  481. EXOFS_ERR("ERROR: exofs_delete_entry:"
  482. "zero-length entry in directory(0x%lx)\n",
  483. inode->i_ino);
  484. err = -EIO;
  485. goto out;
  486. }
  487. pde = de;
  488. de = exofs_next_entry(de);
  489. }
  490. if (pde)
  491. from = (char *)pde - (char *)page_address(page);
  492. pos = page_offset(page) + from;
  493. lock_page(page);
  494. err = exofs_write_begin(NULL, page->mapping, pos, to - from, 0,
  495. &page, NULL);
  496. if (err)
  497. EXOFS_ERR("exofs_delete_entry: exofs_write_begin FAILED => %d\n",
  498. err);
  499. if (pde)
  500. pde->rec_len = cpu_to_le16(to - from);
  501. dir->inode_no = 0;
  502. if (likely(!err))
  503. err = exofs_commit_chunk(page, pos, to - from);
  504. inode->i_ctime = inode->i_mtime = current_time(inode);
  505. mark_inode_dirty(inode);
  506. sbi->s_numfiles--;
  507. out:
  508. exofs_put_page(page);
  509. return err;
  510. }
  511. /* kept aligned on 4 bytes */
  512. #define THIS_DIR ".\0\0"
  513. #define PARENT_DIR "..\0"
  514. int exofs_make_empty(struct inode *inode, struct inode *parent)
  515. {
  516. struct address_space *mapping = inode->i_mapping;
  517. struct page *page = grab_cache_page(mapping, 0);
  518. unsigned chunk_size = exofs_chunk_size(inode);
  519. struct exofs_dir_entry *de;
  520. int err;
  521. void *kaddr;
  522. if (!page)
  523. return -ENOMEM;
  524. err = exofs_write_begin(NULL, page->mapping, 0, chunk_size, 0,
  525. &page, NULL);
  526. if (err) {
  527. unlock_page(page);
  528. goto fail;
  529. }
  530. kaddr = kmap_atomic(page);
  531. de = (struct exofs_dir_entry *)kaddr;
  532. de->name_len = 1;
  533. de->rec_len = cpu_to_le16(EXOFS_DIR_REC_LEN(1));
  534. memcpy(de->name, THIS_DIR, sizeof(THIS_DIR));
  535. de->inode_no = cpu_to_le64(inode->i_ino);
  536. exofs_set_de_type(de, inode);
  537. de = (struct exofs_dir_entry *)(kaddr + EXOFS_DIR_REC_LEN(1));
  538. de->name_len = 2;
  539. de->rec_len = cpu_to_le16(chunk_size - EXOFS_DIR_REC_LEN(1));
  540. de->inode_no = cpu_to_le64(parent->i_ino);
  541. memcpy(de->name, PARENT_DIR, sizeof(PARENT_DIR));
  542. exofs_set_de_type(de, inode);
  543. kunmap_atomic(kaddr);
  544. err = exofs_commit_chunk(page, 0, chunk_size);
  545. fail:
  546. put_page(page);
  547. return err;
  548. }
  549. int exofs_empty_dir(struct inode *inode)
  550. {
  551. struct page *page = NULL;
  552. unsigned long i, npages = dir_pages(inode);
  553. for (i = 0; i < npages; i++) {
  554. char *kaddr;
  555. struct exofs_dir_entry *de;
  556. page = exofs_get_page(inode, i);
  557. if (IS_ERR(page))
  558. continue;
  559. kaddr = page_address(page);
  560. de = (struct exofs_dir_entry *)kaddr;
  561. kaddr += exofs_last_byte(inode, i) - EXOFS_DIR_REC_LEN(1);
  562. while ((char *)de <= kaddr) {
  563. if (de->rec_len == 0) {
  564. EXOFS_ERR("ERROR: exofs_empty_dir: "
  565. "zero-length directory entry"
  566. "kaddr=%p, de=%p\n", kaddr, de);
  567. goto not_empty;
  568. }
  569. if (de->inode_no != 0) {
  570. /* check for . and .. */
  571. if (de->name[0] != '.')
  572. goto not_empty;
  573. if (de->name_len > 2)
  574. goto not_empty;
  575. if (de->name_len < 2) {
  576. if (le64_to_cpu(de->inode_no) !=
  577. inode->i_ino)
  578. goto not_empty;
  579. } else if (de->name[1] != '.')
  580. goto not_empty;
  581. }
  582. de = exofs_next_entry(de);
  583. }
  584. exofs_put_page(page);
  585. }
  586. return 1;
  587. not_empty:
  588. exofs_put_page(page);
  589. return 0;
  590. }
  591. const struct file_operations exofs_dir_operations = {
  592. .llseek = generic_file_llseek,
  593. .read = generic_read_dir,
  594. .iterate_shared = exofs_readdir,
  595. };