dir.c 9.0 KB

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