dir.c 8.4 KB

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