dir.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. */
  4. #include <linux/string.h>
  5. #include <linux/errno.h>
  6. #include <linux/fs.h>
  7. #include <linux/reiserfs_fs.h>
  8. #include <linux/stat.h>
  9. #include <linux/buffer_head.h>
  10. #include <linux/slab.h>
  11. #include <asm/uaccess.h>
  12. extern const struct reiserfs_key MIN_KEY;
  13. static int reiserfs_readdir(struct file *, void *, filldir_t);
  14. static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
  15. int datasync);
  16. const struct file_operations reiserfs_dir_operations = {
  17. .read = generic_read_dir,
  18. .readdir = reiserfs_readdir,
  19. .fsync = reiserfs_dir_fsync,
  20. .unlocked_ioctl = reiserfs_ioctl,
  21. #ifdef CONFIG_COMPAT
  22. .compat_ioctl = reiserfs_compat_ioctl,
  23. #endif
  24. };
  25. static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
  26. int datasync)
  27. {
  28. struct inode *inode = dentry->d_inode;
  29. int err;
  30. reiserfs_write_lock(inode->i_sb);
  31. err = reiserfs_commit_for_inode(inode);
  32. reiserfs_write_unlock(inode->i_sb);
  33. if (err < 0)
  34. return err;
  35. return 0;
  36. }
  37. #define store_ih(where,what) copy_item_head (where, what)
  38. static inline bool is_privroot_deh(struct dentry *dir,
  39. struct reiserfs_de_head *deh)
  40. {
  41. struct dentry *privroot = REISERFS_SB(dir->d_sb)->priv_root;
  42. if (reiserfs_expose_privroot(dir->d_sb))
  43. return 0;
  44. return (dir == dir->d_parent && privroot->d_inode &&
  45. deh->deh_objectid == INODE_PKEY(privroot->d_inode)->k_objectid);
  46. }
  47. int reiserfs_readdir_dentry(struct dentry *dentry, void *dirent,
  48. filldir_t filldir, loff_t *pos)
  49. {
  50. struct inode *inode = dentry->d_inode;
  51. struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
  52. INITIALIZE_PATH(path_to_entry);
  53. struct buffer_head *bh;
  54. int item_num, entry_num;
  55. const struct reiserfs_key *rkey;
  56. struct item_head *ih, tmp_ih;
  57. int search_res;
  58. char *local_buf;
  59. loff_t next_pos;
  60. char small_buf[32]; /* avoid kmalloc if we can */
  61. struct reiserfs_dir_entry de;
  62. int ret = 0;
  63. reiserfs_write_lock(inode->i_sb);
  64. reiserfs_check_lock_depth(inode->i_sb, "readdir");
  65. /* form key for search the next directory entry using f_pos field of
  66. file structure */
  67. make_cpu_key(&pos_key, inode, *pos ?: DOT_OFFSET, TYPE_DIRENTRY, 3);
  68. next_pos = cpu_key_k_offset(&pos_key);
  69. path_to_entry.reada = PATH_READA;
  70. while (1) {
  71. research:
  72. /* search the directory item, containing entry with specified key */
  73. search_res =
  74. search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
  75. &de);
  76. if (search_res == IO_ERROR) {
  77. // FIXME: we could just skip part of directory which could
  78. // not be read
  79. ret = -EIO;
  80. goto out;
  81. }
  82. entry_num = de.de_entry_num;
  83. bh = de.de_bh;
  84. item_num = de.de_item_num;
  85. ih = de.de_ih;
  86. store_ih(&tmp_ih, ih);
  87. /* we must have found item, that is item of this directory, */
  88. RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
  89. "vs-9000: found item %h does not match to dir we readdir %K",
  90. ih, &pos_key);
  91. RFALSE(item_num > B_NR_ITEMS(bh) - 1,
  92. "vs-9005 item_num == %d, item amount == %d",
  93. item_num, B_NR_ITEMS(bh));
  94. /* and entry must be not more than number of entries in the item */
  95. RFALSE(I_ENTRY_COUNT(ih) < entry_num,
  96. "vs-9010: entry number is too big %d (%d)",
  97. entry_num, I_ENTRY_COUNT(ih));
  98. if (search_res == POSITION_FOUND
  99. || entry_num < I_ENTRY_COUNT(ih)) {
  100. /* go through all entries in the directory item beginning from the entry, that has been found */
  101. struct reiserfs_de_head *deh =
  102. B_I_DEH(bh, ih) + entry_num;
  103. for (; entry_num < I_ENTRY_COUNT(ih);
  104. entry_num++, deh++) {
  105. int d_reclen;
  106. char *d_name;
  107. off_t d_off;
  108. ino_t d_ino;
  109. if (!de_visible(deh))
  110. /* it is hidden entry */
  111. continue;
  112. d_reclen = entry_length(bh, ih, entry_num);
  113. d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
  114. if (d_reclen <= 0 ||
  115. d_name + d_reclen > bh->b_data + bh->b_size) {
  116. /* There is corrupted data in entry,
  117. * We'd better stop here */
  118. pathrelse(&path_to_entry);
  119. ret = -EIO;
  120. goto out;
  121. }
  122. if (!d_name[d_reclen - 1])
  123. d_reclen = strlen(d_name);
  124. if (d_reclen >
  125. REISERFS_MAX_NAME(inode->i_sb->
  126. s_blocksize)) {
  127. /* too big to send back to VFS */
  128. continue;
  129. }
  130. /* Ignore the .reiserfs_priv entry */
  131. if (is_privroot_deh(dentry, deh))
  132. continue;
  133. d_off = deh_offset(deh);
  134. *pos = d_off;
  135. d_ino = deh_objectid(deh);
  136. if (d_reclen <= 32) {
  137. local_buf = small_buf;
  138. } else {
  139. local_buf = kmalloc(d_reclen,
  140. GFP_NOFS);
  141. if (!local_buf) {
  142. pathrelse(&path_to_entry);
  143. ret = -ENOMEM;
  144. goto out;
  145. }
  146. if (item_moved(&tmp_ih, &path_to_entry)) {
  147. kfree(local_buf);
  148. goto research;
  149. }
  150. }
  151. // Note, that we copy name to user space via temporary
  152. // buffer (local_buf) because filldir will block if
  153. // user space buffer is swapped out. At that time
  154. // entry can move to somewhere else
  155. memcpy(local_buf, d_name, d_reclen);
  156. /*
  157. * Since filldir might sleep, we can release
  158. * the write lock here for other waiters
  159. */
  160. reiserfs_write_unlock(inode->i_sb);
  161. if (filldir
  162. (dirent, local_buf, d_reclen, d_off, d_ino,
  163. DT_UNKNOWN) < 0) {
  164. reiserfs_write_lock(inode->i_sb);
  165. if (local_buf != small_buf) {
  166. kfree(local_buf);
  167. }
  168. goto end;
  169. }
  170. reiserfs_write_lock(inode->i_sb);
  171. if (local_buf != small_buf) {
  172. kfree(local_buf);
  173. }
  174. // next entry should be looked for with such offset
  175. next_pos = deh_offset(deh) + 1;
  176. if (item_moved(&tmp_ih, &path_to_entry)) {
  177. goto research;
  178. }
  179. } /* for */
  180. }
  181. if (item_num != B_NR_ITEMS(bh) - 1)
  182. // end of directory has been reached
  183. goto end;
  184. /* item we went through is last item of node. Using right
  185. delimiting key check is it directory end */
  186. rkey = get_rkey(&path_to_entry, inode->i_sb);
  187. if (!comp_le_keys(rkey, &MIN_KEY)) {
  188. /* set pos_key to key, that is the smallest and greater
  189. that key of the last entry in the item */
  190. set_cpu_key_k_offset(&pos_key, next_pos);
  191. continue;
  192. }
  193. if (COMP_SHORT_KEYS(rkey, &pos_key)) {
  194. // end of directory has been reached
  195. goto end;
  196. }
  197. /* directory continues in the right neighboring block */
  198. set_cpu_key_k_offset(&pos_key,
  199. le_key_k_offset(KEY_FORMAT_3_5, rkey));
  200. } /* while */
  201. end:
  202. *pos = next_pos;
  203. pathrelse(&path_to_entry);
  204. reiserfs_check_path(&path_to_entry);
  205. out:
  206. reiserfs_write_unlock(inode->i_sb);
  207. return ret;
  208. }
  209. static int reiserfs_readdir(struct file *file, void *dirent, filldir_t filldir)
  210. {
  211. struct dentry *dentry = file->f_path.dentry;
  212. return reiserfs_readdir_dentry(dentry, dirent, filldir, &file->f_pos);
  213. }
  214. /* compose directory item containing "." and ".." entries (entries are
  215. not aligned to 4 byte boundary) */
  216. /* the last four params are LE */
  217. void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
  218. __le32 par_dirid, __le32 par_objid)
  219. {
  220. struct reiserfs_de_head *deh;
  221. memset(body, 0, EMPTY_DIR_SIZE_V1);
  222. deh = (struct reiserfs_de_head *)body;
  223. /* direntry header of "." */
  224. put_deh_offset(&(deh[0]), DOT_OFFSET);
  225. /* these two are from make_le_item_head, and are are LE */
  226. deh[0].deh_dir_id = dirid;
  227. deh[0].deh_objectid = objid;
  228. deh[0].deh_state = 0; /* Endian safe if 0 */
  229. put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
  230. mark_de_visible(&(deh[0]));
  231. /* direntry header of ".." */
  232. put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
  233. /* key of ".." for the root directory */
  234. /* these two are from the inode, and are are LE */
  235. deh[1].deh_dir_id = par_dirid;
  236. deh[1].deh_objectid = par_objid;
  237. deh[1].deh_state = 0; /* Endian safe if 0 */
  238. put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
  239. mark_de_visible(&(deh[1]));
  240. /* copy ".." and "." */
  241. memcpy(body + deh_location(&(deh[0])), ".", 1);
  242. memcpy(body + deh_location(&(deh[1])), "..", 2);
  243. }
  244. /* compose directory item containing "." and ".." entries */
  245. void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
  246. __le32 par_dirid, __le32 par_objid)
  247. {
  248. struct reiserfs_de_head *deh;
  249. memset(body, 0, EMPTY_DIR_SIZE);
  250. deh = (struct reiserfs_de_head *)body;
  251. /* direntry header of "." */
  252. put_deh_offset(&(deh[0]), DOT_OFFSET);
  253. /* these two are from make_le_item_head, and are are LE */
  254. deh[0].deh_dir_id = dirid;
  255. deh[0].deh_objectid = objid;
  256. deh[0].deh_state = 0; /* Endian safe if 0 */
  257. put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
  258. mark_de_visible(&(deh[0]));
  259. /* direntry header of ".." */
  260. put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
  261. /* key of ".." for the root directory */
  262. /* these two are from the inode, and are are LE */
  263. deh[1].deh_dir_id = par_dirid;
  264. deh[1].deh_objectid = par_objid;
  265. deh[1].deh_state = 0; /* Endian safe if 0 */
  266. put_deh_location(&(deh[1]),
  267. deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
  268. mark_de_visible(&(deh[1]));
  269. /* copy ".." and "." */
  270. memcpy(body + deh_location(&(deh[0])), ".", 1);
  271. memcpy(body + deh_location(&(deh[1])), "..", 2);
  272. }