dir.c 8.3 KB

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