dir.c 8.7 KB

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