dir.c 8.8 KB

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