libfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*
  2. * fs/libfs.c
  3. * Library for filesystems writers.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/mount.h>
  8. #include <linux/vfs.h>
  9. #include <linux/mutex.h>
  10. #include <asm/uaccess.h>
  11. int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
  12. struct kstat *stat)
  13. {
  14. struct inode *inode = dentry->d_inode;
  15. generic_fillattr(inode, stat);
  16. stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
  17. return 0;
  18. }
  19. int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
  20. {
  21. buf->f_type = dentry->d_sb->s_magic;
  22. buf->f_bsize = PAGE_CACHE_SIZE;
  23. buf->f_namelen = NAME_MAX;
  24. return 0;
  25. }
  26. /*
  27. * Retaining negative dentries for an in-memory filesystem just wastes
  28. * memory and lookup time: arrange for them to be deleted immediately.
  29. */
  30. static int simple_delete_dentry(struct dentry *dentry)
  31. {
  32. return 1;
  33. }
  34. /*
  35. * Lookup the data. This is trivial - if the dentry didn't already
  36. * exist, we know it is negative. Set d_op to delete negative dentries.
  37. */
  38. struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  39. {
  40. static struct dentry_operations simple_dentry_operations = {
  41. .d_delete = simple_delete_dentry,
  42. };
  43. if (dentry->d_name.len > NAME_MAX)
  44. return ERR_PTR(-ENAMETOOLONG);
  45. dentry->d_op = &simple_dentry_operations;
  46. d_add(dentry, NULL);
  47. return NULL;
  48. }
  49. int simple_sync_file(struct file * file, struct dentry *dentry, int datasync)
  50. {
  51. return 0;
  52. }
  53. int dcache_dir_open(struct inode *inode, struct file *file)
  54. {
  55. static struct qstr cursor_name = {.len = 1, .name = "."};
  56. file->private_data = d_alloc(file->f_path.dentry, &cursor_name);
  57. return file->private_data ? 0 : -ENOMEM;
  58. }
  59. int dcache_dir_close(struct inode *inode, struct file *file)
  60. {
  61. dput(file->private_data);
  62. return 0;
  63. }
  64. loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin)
  65. {
  66. mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
  67. switch (origin) {
  68. case 1:
  69. offset += file->f_pos;
  70. case 0:
  71. if (offset >= 0)
  72. break;
  73. default:
  74. mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
  75. return -EINVAL;
  76. }
  77. if (offset != file->f_pos) {
  78. file->f_pos = offset;
  79. if (file->f_pos >= 2) {
  80. struct list_head *p;
  81. struct dentry *cursor = file->private_data;
  82. loff_t n = file->f_pos - 2;
  83. spin_lock(&dcache_lock);
  84. list_del(&cursor->d_u.d_child);
  85. p = file->f_path.dentry->d_subdirs.next;
  86. while (n && p != &file->f_path.dentry->d_subdirs) {
  87. struct dentry *next;
  88. next = list_entry(p, struct dentry, d_u.d_child);
  89. if (!d_unhashed(next) && next->d_inode)
  90. n--;
  91. p = p->next;
  92. }
  93. list_add_tail(&cursor->d_u.d_child, p);
  94. spin_unlock(&dcache_lock);
  95. }
  96. }
  97. mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
  98. return offset;
  99. }
  100. /* Relationship between i_mode and the DT_xxx types */
  101. static inline unsigned char dt_type(struct inode *inode)
  102. {
  103. return (inode->i_mode >> 12) & 15;
  104. }
  105. /*
  106. * Directory is locked and all positive dentries in it are safe, since
  107. * for ramfs-type trees they can't go away without unlink() or rmdir(),
  108. * both impossible due to the lock on directory.
  109. */
  110. int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
  111. {
  112. struct dentry *dentry = filp->f_path.dentry;
  113. struct dentry *cursor = filp->private_data;
  114. struct list_head *p, *q = &cursor->d_u.d_child;
  115. ino_t ino;
  116. int i = filp->f_pos;
  117. switch (i) {
  118. case 0:
  119. ino = dentry->d_inode->i_ino;
  120. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  121. break;
  122. filp->f_pos++;
  123. i++;
  124. /* fallthrough */
  125. case 1:
  126. ino = parent_ino(dentry);
  127. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  128. break;
  129. filp->f_pos++;
  130. i++;
  131. /* fallthrough */
  132. default:
  133. spin_lock(&dcache_lock);
  134. if (filp->f_pos == 2)
  135. list_move(q, &dentry->d_subdirs);
  136. for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
  137. struct dentry *next;
  138. next = list_entry(p, struct dentry, d_u.d_child);
  139. if (d_unhashed(next) || !next->d_inode)
  140. continue;
  141. spin_unlock(&dcache_lock);
  142. if (filldir(dirent, next->d_name.name, next->d_name.len, filp->f_pos, next->d_inode->i_ino, dt_type(next->d_inode)) < 0)
  143. return 0;
  144. spin_lock(&dcache_lock);
  145. /* next is still alive */
  146. list_move(q, p);
  147. p = q;
  148. filp->f_pos++;
  149. }
  150. spin_unlock(&dcache_lock);
  151. }
  152. return 0;
  153. }
  154. ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
  155. {
  156. return -EISDIR;
  157. }
  158. const struct file_operations simple_dir_operations = {
  159. .open = dcache_dir_open,
  160. .release = dcache_dir_close,
  161. .llseek = dcache_dir_lseek,
  162. .read = generic_read_dir,
  163. .readdir = dcache_readdir,
  164. .fsync = simple_sync_file,
  165. };
  166. const struct inode_operations simple_dir_inode_operations = {
  167. .lookup = simple_lookup,
  168. };
  169. static const struct super_operations simple_super_operations = {
  170. .statfs = simple_statfs,
  171. };
  172. /*
  173. * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
  174. * will never be mountable)
  175. */
  176. int get_sb_pseudo(struct file_system_type *fs_type, char *name,
  177. const struct super_operations *ops, unsigned long magic,
  178. struct vfsmount *mnt)
  179. {
  180. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  181. struct dentry *dentry;
  182. struct inode *root;
  183. struct qstr d_name = {.name = name, .len = strlen(name)};
  184. if (IS_ERR(s))
  185. return PTR_ERR(s);
  186. s->s_flags = MS_NOUSER;
  187. s->s_maxbytes = ~0ULL;
  188. s->s_blocksize = 1024;
  189. s->s_blocksize_bits = 10;
  190. s->s_magic = magic;
  191. s->s_op = ops ? ops : &simple_super_operations;
  192. s->s_time_gran = 1;
  193. root = new_inode(s);
  194. if (!root)
  195. goto Enomem;
  196. root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
  197. root->i_uid = root->i_gid = 0;
  198. root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
  199. dentry = d_alloc(NULL, &d_name);
  200. if (!dentry) {
  201. iput(root);
  202. goto Enomem;
  203. }
  204. dentry->d_sb = s;
  205. dentry->d_parent = dentry;
  206. d_instantiate(dentry, root);
  207. s->s_root = dentry;
  208. s->s_flags |= MS_ACTIVE;
  209. return simple_set_mnt(mnt, s);
  210. Enomem:
  211. up_write(&s->s_umount);
  212. deactivate_super(s);
  213. return -ENOMEM;
  214. }
  215. int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  216. {
  217. struct inode *inode = old_dentry->d_inode;
  218. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  219. inc_nlink(inode);
  220. atomic_inc(&inode->i_count);
  221. dget(dentry);
  222. d_instantiate(dentry, inode);
  223. return 0;
  224. }
  225. static inline int simple_positive(struct dentry *dentry)
  226. {
  227. return dentry->d_inode && !d_unhashed(dentry);
  228. }
  229. int simple_empty(struct dentry *dentry)
  230. {
  231. struct dentry *child;
  232. int ret = 0;
  233. spin_lock(&dcache_lock);
  234. list_for_each_entry(child, &dentry->d_subdirs, d_u.d_child)
  235. if (simple_positive(child))
  236. goto out;
  237. ret = 1;
  238. out:
  239. spin_unlock(&dcache_lock);
  240. return ret;
  241. }
  242. int simple_unlink(struct inode *dir, struct dentry *dentry)
  243. {
  244. struct inode *inode = dentry->d_inode;
  245. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  246. drop_nlink(inode);
  247. dput(dentry);
  248. return 0;
  249. }
  250. int simple_rmdir(struct inode *dir, struct dentry *dentry)
  251. {
  252. if (!simple_empty(dentry))
  253. return -ENOTEMPTY;
  254. drop_nlink(dentry->d_inode);
  255. simple_unlink(dir, dentry);
  256. drop_nlink(dir);
  257. return 0;
  258. }
  259. int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
  260. struct inode *new_dir, struct dentry *new_dentry)
  261. {
  262. struct inode *inode = old_dentry->d_inode;
  263. int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
  264. if (!simple_empty(new_dentry))
  265. return -ENOTEMPTY;
  266. if (new_dentry->d_inode) {
  267. simple_unlink(new_dir, new_dentry);
  268. if (they_are_dirs)
  269. drop_nlink(old_dir);
  270. } else if (they_are_dirs) {
  271. drop_nlink(old_dir);
  272. inc_nlink(new_dir);
  273. }
  274. old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
  275. new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
  276. return 0;
  277. }
  278. int simple_readpage(struct file *file, struct page *page)
  279. {
  280. clear_highpage(page);
  281. flush_dcache_page(page);
  282. SetPageUptodate(page);
  283. unlock_page(page);
  284. return 0;
  285. }
  286. int simple_prepare_write(struct file *file, struct page *page,
  287. unsigned from, unsigned to)
  288. {
  289. if (!PageUptodate(page)) {
  290. if (to - from != PAGE_CACHE_SIZE) {
  291. void *kaddr = kmap_atomic(page, KM_USER0);
  292. memset(kaddr, 0, from);
  293. memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
  294. flush_dcache_page(page);
  295. kunmap_atomic(kaddr, KM_USER0);
  296. }
  297. }
  298. return 0;
  299. }
  300. int simple_commit_write(struct file *file, struct page *page,
  301. unsigned from, unsigned to)
  302. {
  303. struct inode *inode = page->mapping->host;
  304. loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
  305. if (!PageUptodate(page))
  306. SetPageUptodate(page);
  307. /*
  308. * No need to use i_size_read() here, the i_size
  309. * cannot change under us because we hold the i_mutex.
  310. */
  311. if (pos > inode->i_size)
  312. i_size_write(inode, pos);
  313. set_page_dirty(page);
  314. return 0;
  315. }
  316. int simple_fill_super(struct super_block *s, int magic, struct tree_descr *files)
  317. {
  318. struct inode *inode;
  319. struct dentry *root;
  320. struct dentry *dentry;
  321. int i;
  322. s->s_blocksize = PAGE_CACHE_SIZE;
  323. s->s_blocksize_bits = PAGE_CACHE_SHIFT;
  324. s->s_magic = magic;
  325. s->s_op = &simple_super_operations;
  326. s->s_time_gran = 1;
  327. inode = new_inode(s);
  328. if (!inode)
  329. return -ENOMEM;
  330. inode->i_mode = S_IFDIR | 0755;
  331. inode->i_uid = inode->i_gid = 0;
  332. inode->i_blocks = 0;
  333. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  334. inode->i_op = &simple_dir_inode_operations;
  335. inode->i_fop = &simple_dir_operations;
  336. inode->i_nlink = 2;
  337. root = d_alloc_root(inode);
  338. if (!root) {
  339. iput(inode);
  340. return -ENOMEM;
  341. }
  342. for (i = 0; !files->name || files->name[0]; i++, files++) {
  343. if (!files->name)
  344. continue;
  345. dentry = d_alloc_name(root, files->name);
  346. if (!dentry)
  347. goto out;
  348. inode = new_inode(s);
  349. if (!inode)
  350. goto out;
  351. inode->i_mode = S_IFREG | files->mode;
  352. inode->i_uid = inode->i_gid = 0;
  353. inode->i_blocks = 0;
  354. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  355. inode->i_fop = files->ops;
  356. inode->i_ino = i;
  357. d_add(dentry, inode);
  358. }
  359. s->s_root = root;
  360. return 0;
  361. out:
  362. d_genocide(root);
  363. dput(root);
  364. return -ENOMEM;
  365. }
  366. static DEFINE_SPINLOCK(pin_fs_lock);
  367. int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
  368. {
  369. struct vfsmount *mnt = NULL;
  370. spin_lock(&pin_fs_lock);
  371. if (unlikely(!*mount)) {
  372. spin_unlock(&pin_fs_lock);
  373. mnt = vfs_kern_mount(type, 0, type->name, NULL);
  374. if (IS_ERR(mnt))
  375. return PTR_ERR(mnt);
  376. spin_lock(&pin_fs_lock);
  377. if (!*mount)
  378. *mount = mnt;
  379. }
  380. mntget(*mount);
  381. ++*count;
  382. spin_unlock(&pin_fs_lock);
  383. mntput(mnt);
  384. return 0;
  385. }
  386. void simple_release_fs(struct vfsmount **mount, int *count)
  387. {
  388. struct vfsmount *mnt;
  389. spin_lock(&pin_fs_lock);
  390. mnt = *mount;
  391. if (!--*count)
  392. *mount = NULL;
  393. spin_unlock(&pin_fs_lock);
  394. mntput(mnt);
  395. }
  396. ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
  397. const void *from, size_t available)
  398. {
  399. loff_t pos = *ppos;
  400. if (pos < 0)
  401. return -EINVAL;
  402. if (pos >= available)
  403. return 0;
  404. if (count > available - pos)
  405. count = available - pos;
  406. if (copy_to_user(to, from + pos, count))
  407. return -EFAULT;
  408. *ppos = pos + count;
  409. return count;
  410. }
  411. /*
  412. * Transaction based IO.
  413. * The file expects a single write which triggers the transaction, and then
  414. * possibly a read which collects the result - which is stored in a
  415. * file-local buffer.
  416. */
  417. char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
  418. {
  419. struct simple_transaction_argresp *ar;
  420. static DEFINE_SPINLOCK(simple_transaction_lock);
  421. if (size > SIMPLE_TRANSACTION_LIMIT - 1)
  422. return ERR_PTR(-EFBIG);
  423. ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
  424. if (!ar)
  425. return ERR_PTR(-ENOMEM);
  426. spin_lock(&simple_transaction_lock);
  427. /* only one write allowed per open */
  428. if (file->private_data) {
  429. spin_unlock(&simple_transaction_lock);
  430. free_page((unsigned long)ar);
  431. return ERR_PTR(-EBUSY);
  432. }
  433. file->private_data = ar;
  434. spin_unlock(&simple_transaction_lock);
  435. if (copy_from_user(ar->data, buf, size))
  436. return ERR_PTR(-EFAULT);
  437. return ar->data;
  438. }
  439. ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  440. {
  441. struct simple_transaction_argresp *ar = file->private_data;
  442. if (!ar)
  443. return 0;
  444. return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
  445. }
  446. int simple_transaction_release(struct inode *inode, struct file *file)
  447. {
  448. free_page((unsigned long)file->private_data);
  449. return 0;
  450. }
  451. /* Simple attribute files */
  452. struct simple_attr {
  453. u64 (*get)(void *);
  454. void (*set)(void *, u64);
  455. char get_buf[24]; /* enough to store a u64 and "\n\0" */
  456. char set_buf[24];
  457. void *data;
  458. const char *fmt; /* format for read operation */
  459. struct mutex mutex; /* protects access to these buffers */
  460. };
  461. /* simple_attr_open is called by an actual attribute open file operation
  462. * to set the attribute specific access operations. */
  463. int simple_attr_open(struct inode *inode, struct file *file,
  464. u64 (*get)(void *), void (*set)(void *, u64),
  465. const char *fmt)
  466. {
  467. struct simple_attr *attr;
  468. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  469. if (!attr)
  470. return -ENOMEM;
  471. attr->get = get;
  472. attr->set = set;
  473. attr->data = inode->i_private;
  474. attr->fmt = fmt;
  475. mutex_init(&attr->mutex);
  476. file->private_data = attr;
  477. return nonseekable_open(inode, file);
  478. }
  479. int simple_attr_close(struct inode *inode, struct file *file)
  480. {
  481. kfree(file->private_data);
  482. return 0;
  483. }
  484. /* read from the buffer that is filled with the get function */
  485. ssize_t simple_attr_read(struct file *file, char __user *buf,
  486. size_t len, loff_t *ppos)
  487. {
  488. struct simple_attr *attr;
  489. size_t size;
  490. ssize_t ret;
  491. attr = file->private_data;
  492. if (!attr->get)
  493. return -EACCES;
  494. mutex_lock(&attr->mutex);
  495. if (*ppos) /* continued read */
  496. size = strlen(attr->get_buf);
  497. else /* first read */
  498. size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
  499. attr->fmt,
  500. (unsigned long long)attr->get(attr->data));
  501. ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
  502. mutex_unlock(&attr->mutex);
  503. return ret;
  504. }
  505. /* interpret the buffer as a number to call the set function with */
  506. ssize_t simple_attr_write(struct file *file, const char __user *buf,
  507. size_t len, loff_t *ppos)
  508. {
  509. struct simple_attr *attr;
  510. u64 val;
  511. size_t size;
  512. ssize_t ret;
  513. attr = file->private_data;
  514. if (!attr->set)
  515. return -EACCES;
  516. mutex_lock(&attr->mutex);
  517. ret = -EFAULT;
  518. size = min(sizeof(attr->set_buf) - 1, len);
  519. if (copy_from_user(attr->set_buf, buf, size))
  520. goto out;
  521. ret = len; /* claim we got the whole input */
  522. attr->set_buf[size] = '\0';
  523. val = simple_strtol(attr->set_buf, NULL, 0);
  524. attr->set(attr->data, val);
  525. out:
  526. mutex_unlock(&attr->mutex);
  527. return ret;
  528. }
  529. EXPORT_SYMBOL(dcache_dir_close);
  530. EXPORT_SYMBOL(dcache_dir_lseek);
  531. EXPORT_SYMBOL(dcache_dir_open);
  532. EXPORT_SYMBOL(dcache_readdir);
  533. EXPORT_SYMBOL(generic_read_dir);
  534. EXPORT_SYMBOL(get_sb_pseudo);
  535. EXPORT_SYMBOL(simple_commit_write);
  536. EXPORT_SYMBOL(simple_dir_inode_operations);
  537. EXPORT_SYMBOL(simple_dir_operations);
  538. EXPORT_SYMBOL(simple_empty);
  539. EXPORT_SYMBOL(d_alloc_name);
  540. EXPORT_SYMBOL(simple_fill_super);
  541. EXPORT_SYMBOL(simple_getattr);
  542. EXPORT_SYMBOL(simple_link);
  543. EXPORT_SYMBOL(simple_lookup);
  544. EXPORT_SYMBOL(simple_pin_fs);
  545. EXPORT_SYMBOL(simple_prepare_write);
  546. EXPORT_SYMBOL(simple_readpage);
  547. EXPORT_SYMBOL(simple_release_fs);
  548. EXPORT_SYMBOL(simple_rename);
  549. EXPORT_SYMBOL(simple_rmdir);
  550. EXPORT_SYMBOL(simple_statfs);
  551. EXPORT_SYMBOL(simple_sync_file);
  552. EXPORT_SYMBOL(simple_unlink);
  553. EXPORT_SYMBOL(simple_read_from_buffer);
  554. EXPORT_SYMBOL(simple_transaction_get);
  555. EXPORT_SYMBOL(simple_transaction_read);
  556. EXPORT_SYMBOL(simple_transaction_release);
  557. EXPORT_SYMBOL_GPL(simple_attr_open);
  558. EXPORT_SYMBOL_GPL(simple_attr_close);
  559. EXPORT_SYMBOL_GPL(simple_attr_read);
  560. EXPORT_SYMBOL_GPL(simple_attr_write);