dir.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * fs/bfs/dir.c
  3. * BFS directory operations.
  4. * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
  5. * Made endianness-clean by Andrew Stribblehill <ads@wompom.org> 2005
  6. */
  7. #include <linux/time.h>
  8. #include <linux/string.h>
  9. #include <linux/fs.h>
  10. #include <linux/buffer_head.h>
  11. #include <linux/sched.h>
  12. #include "bfs.h"
  13. #undef DEBUG
  14. #ifdef DEBUG
  15. #define dprintf(x...) printf(x)
  16. #else
  17. #define dprintf(x...)
  18. #endif
  19. static int bfs_add_entry(struct inode *dir, const unsigned char *name,
  20. int namelen, int ino);
  21. static struct buffer_head *bfs_find_entry(struct inode *dir,
  22. const unsigned char *name, int namelen,
  23. struct bfs_dirent **res_dir);
  24. static int bfs_readdir(struct file *f, struct dir_context *ctx)
  25. {
  26. struct inode *dir = file_inode(f);
  27. struct buffer_head *bh;
  28. struct bfs_dirent *de;
  29. unsigned int offset;
  30. int block;
  31. if (ctx->pos & (BFS_DIRENT_SIZE - 1)) {
  32. printf("Bad f_pos=%08lx for %s:%08lx\n",
  33. (unsigned long)ctx->pos,
  34. dir->i_sb->s_id, dir->i_ino);
  35. return -EINVAL;
  36. }
  37. while (ctx->pos < dir->i_size) {
  38. offset = ctx->pos & (BFS_BSIZE - 1);
  39. block = BFS_I(dir)->i_sblock + (ctx->pos >> BFS_BSIZE_BITS);
  40. bh = sb_bread(dir->i_sb, block);
  41. if (!bh) {
  42. ctx->pos += BFS_BSIZE - offset;
  43. continue;
  44. }
  45. do {
  46. de = (struct bfs_dirent *)(bh->b_data + offset);
  47. if (de->ino) {
  48. int size = strnlen(de->name, BFS_NAMELEN);
  49. if (!dir_emit(ctx, de->name, size,
  50. le16_to_cpu(de->ino),
  51. DT_UNKNOWN)) {
  52. brelse(bh);
  53. return 0;
  54. }
  55. }
  56. offset += BFS_DIRENT_SIZE;
  57. ctx->pos += BFS_DIRENT_SIZE;
  58. } while ((offset < BFS_BSIZE) && (ctx->pos < dir->i_size));
  59. brelse(bh);
  60. }
  61. return 0;
  62. }
  63. const struct file_operations bfs_dir_operations = {
  64. .read = generic_read_dir,
  65. .iterate_shared = bfs_readdir,
  66. .fsync = generic_file_fsync,
  67. .llseek = generic_file_llseek,
  68. };
  69. static int bfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  70. bool excl)
  71. {
  72. int err;
  73. struct inode *inode;
  74. struct super_block *s = dir->i_sb;
  75. struct bfs_sb_info *info = BFS_SB(s);
  76. unsigned long ino;
  77. inode = new_inode(s);
  78. if (!inode)
  79. return -ENOMEM;
  80. mutex_lock(&info->bfs_lock);
  81. ino = find_first_zero_bit(info->si_imap, info->si_lasti + 1);
  82. if (ino > info->si_lasti) {
  83. mutex_unlock(&info->bfs_lock);
  84. iput(inode);
  85. return -ENOSPC;
  86. }
  87. set_bit(ino, info->si_imap);
  88. info->si_freei--;
  89. inode_init_owner(inode, dir, mode);
  90. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  91. inode->i_blocks = 0;
  92. inode->i_op = &bfs_file_inops;
  93. inode->i_fop = &bfs_file_operations;
  94. inode->i_mapping->a_ops = &bfs_aops;
  95. inode->i_ino = ino;
  96. BFS_I(inode)->i_dsk_ino = ino;
  97. BFS_I(inode)->i_sblock = 0;
  98. BFS_I(inode)->i_eblock = 0;
  99. insert_inode_hash(inode);
  100. mark_inode_dirty(inode);
  101. bfs_dump_imap("create", s);
  102. err = bfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len,
  103. inode->i_ino);
  104. if (err) {
  105. inode_dec_link_count(inode);
  106. mutex_unlock(&info->bfs_lock);
  107. iput(inode);
  108. return err;
  109. }
  110. mutex_unlock(&info->bfs_lock);
  111. d_instantiate(dentry, inode);
  112. return 0;
  113. }
  114. static struct dentry *bfs_lookup(struct inode *dir, struct dentry *dentry,
  115. unsigned int flags)
  116. {
  117. struct inode *inode = NULL;
  118. struct buffer_head *bh;
  119. struct bfs_dirent *de;
  120. struct bfs_sb_info *info = BFS_SB(dir->i_sb);
  121. if (dentry->d_name.len > BFS_NAMELEN)
  122. return ERR_PTR(-ENAMETOOLONG);
  123. mutex_lock(&info->bfs_lock);
  124. bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
  125. if (bh) {
  126. unsigned long ino = (unsigned long)le16_to_cpu(de->ino);
  127. brelse(bh);
  128. inode = bfs_iget(dir->i_sb, ino);
  129. if (IS_ERR(inode)) {
  130. mutex_unlock(&info->bfs_lock);
  131. return ERR_CAST(inode);
  132. }
  133. }
  134. mutex_unlock(&info->bfs_lock);
  135. d_add(dentry, inode);
  136. return NULL;
  137. }
  138. static int bfs_link(struct dentry *old, struct inode *dir,
  139. struct dentry *new)
  140. {
  141. struct inode *inode = d_inode(old);
  142. struct bfs_sb_info *info = BFS_SB(inode->i_sb);
  143. int err;
  144. mutex_lock(&info->bfs_lock);
  145. err = bfs_add_entry(dir, new->d_name.name, new->d_name.len,
  146. inode->i_ino);
  147. if (err) {
  148. mutex_unlock(&info->bfs_lock);
  149. return err;
  150. }
  151. inc_nlink(inode);
  152. inode->i_ctime = current_time(inode);
  153. mark_inode_dirty(inode);
  154. ihold(inode);
  155. d_instantiate(new, inode);
  156. mutex_unlock(&info->bfs_lock);
  157. return 0;
  158. }
  159. static int bfs_unlink(struct inode *dir, struct dentry *dentry)
  160. {
  161. int error = -ENOENT;
  162. struct inode *inode = d_inode(dentry);
  163. struct buffer_head *bh;
  164. struct bfs_dirent *de;
  165. struct bfs_sb_info *info = BFS_SB(inode->i_sb);
  166. mutex_lock(&info->bfs_lock);
  167. bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
  168. if (!bh || (le16_to_cpu(de->ino) != inode->i_ino))
  169. goto out_brelse;
  170. if (!inode->i_nlink) {
  171. printf("unlinking non-existent file %s:%lu (nlink=%d)\n",
  172. inode->i_sb->s_id, inode->i_ino,
  173. inode->i_nlink);
  174. set_nlink(inode, 1);
  175. }
  176. de->ino = 0;
  177. mark_buffer_dirty_inode(bh, dir);
  178. dir->i_ctime = dir->i_mtime = current_time(dir);
  179. mark_inode_dirty(dir);
  180. inode->i_ctime = dir->i_ctime;
  181. inode_dec_link_count(inode);
  182. error = 0;
  183. out_brelse:
  184. brelse(bh);
  185. mutex_unlock(&info->bfs_lock);
  186. return error;
  187. }
  188. static int bfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  189. struct inode *new_dir, struct dentry *new_dentry,
  190. unsigned int flags)
  191. {
  192. struct inode *old_inode, *new_inode;
  193. struct buffer_head *old_bh, *new_bh;
  194. struct bfs_dirent *old_de, *new_de;
  195. struct bfs_sb_info *info;
  196. int error = -ENOENT;
  197. if (flags & ~RENAME_NOREPLACE)
  198. return -EINVAL;
  199. old_bh = new_bh = NULL;
  200. old_inode = d_inode(old_dentry);
  201. if (S_ISDIR(old_inode->i_mode))
  202. return -EINVAL;
  203. info = BFS_SB(old_inode->i_sb);
  204. mutex_lock(&info->bfs_lock);
  205. old_bh = bfs_find_entry(old_dir,
  206. old_dentry->d_name.name,
  207. old_dentry->d_name.len, &old_de);
  208. if (!old_bh || (le16_to_cpu(old_de->ino) != old_inode->i_ino))
  209. goto end_rename;
  210. error = -EPERM;
  211. new_inode = d_inode(new_dentry);
  212. new_bh = bfs_find_entry(new_dir,
  213. new_dentry->d_name.name,
  214. new_dentry->d_name.len, &new_de);
  215. if (new_bh && !new_inode) {
  216. brelse(new_bh);
  217. new_bh = NULL;
  218. }
  219. if (!new_bh) {
  220. error = bfs_add_entry(new_dir,
  221. new_dentry->d_name.name,
  222. new_dentry->d_name.len,
  223. old_inode->i_ino);
  224. if (error)
  225. goto end_rename;
  226. }
  227. old_de->ino = 0;
  228. old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
  229. mark_inode_dirty(old_dir);
  230. if (new_inode) {
  231. new_inode->i_ctime = current_time(new_inode);
  232. inode_dec_link_count(new_inode);
  233. }
  234. mark_buffer_dirty_inode(old_bh, old_dir);
  235. error = 0;
  236. end_rename:
  237. mutex_unlock(&info->bfs_lock);
  238. brelse(old_bh);
  239. brelse(new_bh);
  240. return error;
  241. }
  242. const struct inode_operations bfs_dir_inops = {
  243. .create = bfs_create,
  244. .lookup = bfs_lookup,
  245. .link = bfs_link,
  246. .unlink = bfs_unlink,
  247. .rename = bfs_rename,
  248. };
  249. static int bfs_add_entry(struct inode *dir, const unsigned char *name,
  250. int namelen, int ino)
  251. {
  252. struct buffer_head *bh;
  253. struct bfs_dirent *de;
  254. int block, sblock, eblock, off, pos;
  255. int i;
  256. dprintf("name=%s, namelen=%d\n", name, namelen);
  257. if (!namelen)
  258. return -ENOENT;
  259. if (namelen > BFS_NAMELEN)
  260. return -ENAMETOOLONG;
  261. sblock = BFS_I(dir)->i_sblock;
  262. eblock = BFS_I(dir)->i_eblock;
  263. for (block = sblock; block <= eblock; block++) {
  264. bh = sb_bread(dir->i_sb, block);
  265. if (!bh)
  266. return -EIO;
  267. for (off = 0; off < BFS_BSIZE; off += BFS_DIRENT_SIZE) {
  268. de = (struct bfs_dirent *)(bh->b_data + off);
  269. if (!de->ino) {
  270. pos = (block - sblock) * BFS_BSIZE + off;
  271. if (pos >= dir->i_size) {
  272. dir->i_size += BFS_DIRENT_SIZE;
  273. dir->i_ctime = current_time(dir);
  274. }
  275. dir->i_mtime = current_time(dir);
  276. mark_inode_dirty(dir);
  277. de->ino = cpu_to_le16((u16)ino);
  278. for (i = 0; i < BFS_NAMELEN; i++)
  279. de->name[i] =
  280. (i < namelen) ? name[i] : 0;
  281. mark_buffer_dirty_inode(bh, dir);
  282. brelse(bh);
  283. return 0;
  284. }
  285. }
  286. brelse(bh);
  287. }
  288. return -ENOSPC;
  289. }
  290. static inline int bfs_namecmp(int len, const unsigned char *name,
  291. const char *buffer)
  292. {
  293. if ((len < BFS_NAMELEN) && buffer[len])
  294. return 0;
  295. return !memcmp(name, buffer, len);
  296. }
  297. static struct buffer_head *bfs_find_entry(struct inode *dir,
  298. const unsigned char *name, int namelen,
  299. struct bfs_dirent **res_dir)
  300. {
  301. unsigned long block = 0, offset = 0;
  302. struct buffer_head *bh = NULL;
  303. struct bfs_dirent *de;
  304. *res_dir = NULL;
  305. if (namelen > BFS_NAMELEN)
  306. return NULL;
  307. while (block * BFS_BSIZE + offset < dir->i_size) {
  308. if (!bh) {
  309. bh = sb_bread(dir->i_sb, BFS_I(dir)->i_sblock + block);
  310. if (!bh) {
  311. block++;
  312. continue;
  313. }
  314. }
  315. de = (struct bfs_dirent *)(bh->b_data + offset);
  316. offset += BFS_DIRENT_SIZE;
  317. if (le16_to_cpu(de->ino) &&
  318. bfs_namecmp(namelen, name, de->name)) {
  319. *res_dir = de;
  320. return bh;
  321. }
  322. if (offset < bh->b_size)
  323. continue;
  324. brelse(bh);
  325. bh = NULL;
  326. offset = 0;
  327. block++;
  328. }
  329. brelse(bh);
  330. return NULL;
  331. }