dir.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/bfs/dir.c
  4. * BFS directory operations.
  5. * Copyright (C) 1999-2018 Tigran Aivazian <aivazian.tigran@gmail.com>
  6. * Made endianness-clean by Andrew Stribblehill <ads@wompom.org> 2005
  7. */
  8. #include <linux/time.h>
  9. #include <linux/string.h>
  10. #include <linux/fs.h>
  11. #include <linux/buffer_head.h>
  12. #include <linux/sched.h>
  13. #include "bfs.h"
  14. #undef DEBUG
  15. #ifdef DEBUG
  16. #define dprintf(x...) printf(x)
  17. #else
  18. #define dprintf(x...)
  19. #endif
  20. static int bfs_add_entry(struct inode *dir, const struct qstr *child, int ino);
  21. static struct buffer_head *bfs_find_entry(struct inode *dir,
  22. const struct qstr *child,
  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, inode->i_ino);
  103. if (err) {
  104. inode_dec_link_count(inode);
  105. mutex_unlock(&info->bfs_lock);
  106. iput(inode);
  107. return err;
  108. }
  109. mutex_unlock(&info->bfs_lock);
  110. d_instantiate(dentry, inode);
  111. return 0;
  112. }
  113. static struct dentry *bfs_lookup(struct inode *dir, struct dentry *dentry,
  114. unsigned int flags)
  115. {
  116. struct inode *inode = NULL;
  117. struct buffer_head *bh;
  118. struct bfs_dirent *de;
  119. struct bfs_sb_info *info = BFS_SB(dir->i_sb);
  120. if (dentry->d_name.len > BFS_NAMELEN)
  121. return ERR_PTR(-ENAMETOOLONG);
  122. mutex_lock(&info->bfs_lock);
  123. bh = bfs_find_entry(dir, &dentry->d_name, &de);
  124. if (bh) {
  125. unsigned long ino = (unsigned long)le16_to_cpu(de->ino);
  126. brelse(bh);
  127. inode = bfs_iget(dir->i_sb, ino);
  128. }
  129. mutex_unlock(&info->bfs_lock);
  130. return d_splice_alias(inode, dentry);
  131. }
  132. static int bfs_link(struct dentry *old, struct inode *dir,
  133. struct dentry *new)
  134. {
  135. struct inode *inode = d_inode(old);
  136. struct bfs_sb_info *info = BFS_SB(inode->i_sb);
  137. int err;
  138. mutex_lock(&info->bfs_lock);
  139. err = bfs_add_entry(dir, &new->d_name, inode->i_ino);
  140. if (err) {
  141. mutex_unlock(&info->bfs_lock);
  142. return err;
  143. }
  144. inc_nlink(inode);
  145. inode->i_ctime = current_time(inode);
  146. mark_inode_dirty(inode);
  147. ihold(inode);
  148. d_instantiate(new, inode);
  149. mutex_unlock(&info->bfs_lock);
  150. return 0;
  151. }
  152. static int bfs_unlink(struct inode *dir, struct dentry *dentry)
  153. {
  154. int error = -ENOENT;
  155. struct inode *inode = d_inode(dentry);
  156. struct buffer_head *bh;
  157. struct bfs_dirent *de;
  158. struct bfs_sb_info *info = BFS_SB(inode->i_sb);
  159. mutex_lock(&info->bfs_lock);
  160. bh = bfs_find_entry(dir, &dentry->d_name, &de);
  161. if (!bh || (le16_to_cpu(de->ino) != inode->i_ino))
  162. goto out_brelse;
  163. if (!inode->i_nlink) {
  164. printf("unlinking non-existent file %s:%lu (nlink=%d)\n",
  165. inode->i_sb->s_id, inode->i_ino,
  166. inode->i_nlink);
  167. set_nlink(inode, 1);
  168. }
  169. de->ino = 0;
  170. mark_buffer_dirty_inode(bh, dir);
  171. dir->i_ctime = dir->i_mtime = current_time(dir);
  172. mark_inode_dirty(dir);
  173. inode->i_ctime = dir->i_ctime;
  174. inode_dec_link_count(inode);
  175. error = 0;
  176. out_brelse:
  177. brelse(bh);
  178. mutex_unlock(&info->bfs_lock);
  179. return error;
  180. }
  181. static int bfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  182. struct inode *new_dir, struct dentry *new_dentry,
  183. unsigned int flags)
  184. {
  185. struct inode *old_inode, *new_inode;
  186. struct buffer_head *old_bh, *new_bh;
  187. struct bfs_dirent *old_de, *new_de;
  188. struct bfs_sb_info *info;
  189. int error = -ENOENT;
  190. if (flags & ~RENAME_NOREPLACE)
  191. return -EINVAL;
  192. old_bh = new_bh = NULL;
  193. old_inode = d_inode(old_dentry);
  194. if (S_ISDIR(old_inode->i_mode))
  195. return -EINVAL;
  196. info = BFS_SB(old_inode->i_sb);
  197. mutex_lock(&info->bfs_lock);
  198. old_bh = bfs_find_entry(old_dir, &old_dentry->d_name, &old_de);
  199. if (!old_bh || (le16_to_cpu(old_de->ino) != old_inode->i_ino))
  200. goto end_rename;
  201. error = -EPERM;
  202. new_inode = d_inode(new_dentry);
  203. new_bh = bfs_find_entry(new_dir, &new_dentry->d_name, &new_de);
  204. if (new_bh && !new_inode) {
  205. brelse(new_bh);
  206. new_bh = NULL;
  207. }
  208. if (!new_bh) {
  209. error = bfs_add_entry(new_dir, &new_dentry->d_name,
  210. old_inode->i_ino);
  211. if (error)
  212. goto end_rename;
  213. }
  214. old_de->ino = 0;
  215. old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
  216. mark_inode_dirty(old_dir);
  217. if (new_inode) {
  218. new_inode->i_ctime = current_time(new_inode);
  219. inode_dec_link_count(new_inode);
  220. }
  221. mark_buffer_dirty_inode(old_bh, old_dir);
  222. error = 0;
  223. end_rename:
  224. mutex_unlock(&info->bfs_lock);
  225. brelse(old_bh);
  226. brelse(new_bh);
  227. return error;
  228. }
  229. const struct inode_operations bfs_dir_inops = {
  230. .create = bfs_create,
  231. .lookup = bfs_lookup,
  232. .link = bfs_link,
  233. .unlink = bfs_unlink,
  234. .rename = bfs_rename,
  235. };
  236. static int bfs_add_entry(struct inode *dir, const struct qstr *child, int ino)
  237. {
  238. const unsigned char *name = child->name;
  239. int namelen = child->len;
  240. struct buffer_head *bh;
  241. struct bfs_dirent *de;
  242. int block, sblock, eblock, off, pos;
  243. int i;
  244. dprintf("name=%s, namelen=%d\n", name, namelen);
  245. if (!namelen)
  246. return -ENOENT;
  247. if (namelen > BFS_NAMELEN)
  248. return -ENAMETOOLONG;
  249. sblock = BFS_I(dir)->i_sblock;
  250. eblock = BFS_I(dir)->i_eblock;
  251. for (block = sblock; block <= eblock; block++) {
  252. bh = sb_bread(dir->i_sb, block);
  253. if (!bh)
  254. return -EIO;
  255. for (off = 0; off < BFS_BSIZE; off += BFS_DIRENT_SIZE) {
  256. de = (struct bfs_dirent *)(bh->b_data + off);
  257. if (!de->ino) {
  258. pos = (block - sblock) * BFS_BSIZE + off;
  259. if (pos >= dir->i_size) {
  260. dir->i_size += BFS_DIRENT_SIZE;
  261. dir->i_ctime = current_time(dir);
  262. }
  263. dir->i_mtime = current_time(dir);
  264. mark_inode_dirty(dir);
  265. de->ino = cpu_to_le16((u16)ino);
  266. for (i = 0; i < BFS_NAMELEN; i++)
  267. de->name[i] =
  268. (i < namelen) ? name[i] : 0;
  269. mark_buffer_dirty_inode(bh, dir);
  270. brelse(bh);
  271. return 0;
  272. }
  273. }
  274. brelse(bh);
  275. }
  276. return -ENOSPC;
  277. }
  278. static inline int bfs_namecmp(int len, const unsigned char *name,
  279. const char *buffer)
  280. {
  281. if ((len < BFS_NAMELEN) && buffer[len])
  282. return 0;
  283. return !memcmp(name, buffer, len);
  284. }
  285. static struct buffer_head *bfs_find_entry(struct inode *dir,
  286. const struct qstr *child,
  287. struct bfs_dirent **res_dir)
  288. {
  289. unsigned long block = 0, offset = 0;
  290. struct buffer_head *bh = NULL;
  291. struct bfs_dirent *de;
  292. const unsigned char *name = child->name;
  293. int namelen = child->len;
  294. *res_dir = NULL;
  295. if (namelen > BFS_NAMELEN)
  296. return NULL;
  297. while (block * BFS_BSIZE + offset < dir->i_size) {
  298. if (!bh) {
  299. bh = sb_bread(dir->i_sb, BFS_I(dir)->i_sblock + block);
  300. if (!bh) {
  301. block++;
  302. continue;
  303. }
  304. }
  305. de = (struct bfs_dirent *)(bh->b_data + offset);
  306. offset += BFS_DIRENT_SIZE;
  307. if (le16_to_cpu(de->ino) &&
  308. bfs_namecmp(namelen, name, de->name)) {
  309. *res_dir = de;
  310. return bh;
  311. }
  312. if (offset < bh->b_size)
  313. continue;
  314. brelse(bh);
  315. bh = NULL;
  316. offset = 0;
  317. block++;
  318. }
  319. brelse(bh);
  320. return NULL;
  321. }