dir.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 = 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_SEC;
  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_SEC;
  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_SEC;
  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. {
  191. struct inode *old_inode, *new_inode;
  192. struct buffer_head *old_bh, *new_bh;
  193. struct bfs_dirent *old_de, *new_de;
  194. struct bfs_sb_info *info;
  195. int error = -ENOENT;
  196. old_bh = new_bh = NULL;
  197. old_inode = d_inode(old_dentry);
  198. if (S_ISDIR(old_inode->i_mode))
  199. return -EINVAL;
  200. info = BFS_SB(old_inode->i_sb);
  201. mutex_lock(&info->bfs_lock);
  202. old_bh = bfs_find_entry(old_dir,
  203. old_dentry->d_name.name,
  204. old_dentry->d_name.len, &old_de);
  205. if (!old_bh || (le16_to_cpu(old_de->ino) != old_inode->i_ino))
  206. goto end_rename;
  207. error = -EPERM;
  208. new_inode = d_inode(new_dentry);
  209. new_bh = bfs_find_entry(new_dir,
  210. new_dentry->d_name.name,
  211. new_dentry->d_name.len, &new_de);
  212. if (new_bh && !new_inode) {
  213. brelse(new_bh);
  214. new_bh = NULL;
  215. }
  216. if (!new_bh) {
  217. error = bfs_add_entry(new_dir,
  218. new_dentry->d_name.name,
  219. new_dentry->d_name.len,
  220. old_inode->i_ino);
  221. if (error)
  222. goto end_rename;
  223. }
  224. old_de->ino = 0;
  225. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC;
  226. mark_inode_dirty(old_dir);
  227. if (new_inode) {
  228. new_inode->i_ctime = CURRENT_TIME_SEC;
  229. inode_dec_link_count(new_inode);
  230. }
  231. mark_buffer_dirty_inode(old_bh, old_dir);
  232. error = 0;
  233. end_rename:
  234. mutex_unlock(&info->bfs_lock);
  235. brelse(old_bh);
  236. brelse(new_bh);
  237. return error;
  238. }
  239. const struct inode_operations bfs_dir_inops = {
  240. .create = bfs_create,
  241. .lookup = bfs_lookup,
  242. .link = bfs_link,
  243. .unlink = bfs_unlink,
  244. .rename = bfs_rename,
  245. };
  246. static int bfs_add_entry(struct inode *dir, const unsigned char *name,
  247. int namelen, int ino)
  248. {
  249. struct buffer_head *bh;
  250. struct bfs_dirent *de;
  251. int block, sblock, eblock, off, pos;
  252. int i;
  253. dprintf("name=%s, namelen=%d\n", name, namelen);
  254. if (!namelen)
  255. return -ENOENT;
  256. if (namelen > BFS_NAMELEN)
  257. return -ENAMETOOLONG;
  258. sblock = BFS_I(dir)->i_sblock;
  259. eblock = BFS_I(dir)->i_eblock;
  260. for (block = sblock; block <= eblock; block++) {
  261. bh = sb_bread(dir->i_sb, block);
  262. if (!bh)
  263. return -EIO;
  264. for (off = 0; off < BFS_BSIZE; off += BFS_DIRENT_SIZE) {
  265. de = (struct bfs_dirent *)(bh->b_data + off);
  266. if (!de->ino) {
  267. pos = (block - sblock) * BFS_BSIZE + off;
  268. if (pos >= dir->i_size) {
  269. dir->i_size += BFS_DIRENT_SIZE;
  270. dir->i_ctime = CURRENT_TIME_SEC;
  271. }
  272. dir->i_mtime = CURRENT_TIME_SEC;
  273. mark_inode_dirty(dir);
  274. de->ino = cpu_to_le16((u16)ino);
  275. for (i = 0; i < BFS_NAMELEN; i++)
  276. de->name[i] =
  277. (i < namelen) ? name[i] : 0;
  278. mark_buffer_dirty_inode(bh, dir);
  279. brelse(bh);
  280. return 0;
  281. }
  282. }
  283. brelse(bh);
  284. }
  285. return -ENOSPC;
  286. }
  287. static inline int bfs_namecmp(int len, const unsigned char *name,
  288. const char *buffer)
  289. {
  290. if ((len < BFS_NAMELEN) && buffer[len])
  291. return 0;
  292. return !memcmp(name, buffer, len);
  293. }
  294. static struct buffer_head *bfs_find_entry(struct inode *dir,
  295. const unsigned char *name, int namelen,
  296. struct bfs_dirent **res_dir)
  297. {
  298. unsigned long block = 0, offset = 0;
  299. struct buffer_head *bh = NULL;
  300. struct bfs_dirent *de;
  301. *res_dir = NULL;
  302. if (namelen > BFS_NAMELEN)
  303. return NULL;
  304. while (block * BFS_BSIZE + offset < dir->i_size) {
  305. if (!bh) {
  306. bh = sb_bread(dir->i_sb, BFS_I(dir)->i_sblock + block);
  307. if (!bh) {
  308. block++;
  309. continue;
  310. }
  311. }
  312. de = (struct bfs_dirent *)(bh->b_data + offset);
  313. offset += BFS_DIRENT_SIZE;
  314. if (le16_to_cpu(de->ino) &&
  315. bfs_namecmp(namelen, name, de->name)) {
  316. *res_dir = de;
  317. return bh;
  318. }
  319. if (offset < bh->b_size)
  320. continue;
  321. brelse(bh);
  322. bh = NULL;
  323. offset = 0;
  324. block++;
  325. }
  326. brelse(bh);
  327. return NULL;
  328. }