dir.c 8.4 KB

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