dir.c 8.9 KB

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