dir.c 9.0 KB

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