dir.c 9.1 KB

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