dir.c 8.6 KB

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