dir.c 8.7 KB

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