libfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 super_block *sb, struct kstatfs *buf)
  20. {
  21. buf->f_type = 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_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_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_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_dentry->d_subdirs.next;
  86. while (n && p != &file->f_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_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_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_del(q);
  136. list_add(q, &dentry->d_subdirs);
  137. }
  138. for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
  139. struct dentry *next;
  140. next = list_entry(p, struct dentry, d_u.d_child);
  141. if (d_unhashed(next) || !next->d_inode)
  142. continue;
  143. spin_unlock(&dcache_lock);
  144. 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)
  145. return 0;
  146. spin_lock(&dcache_lock);
  147. /* next is still alive */
  148. list_del(q);
  149. list_add(q, p);
  150. p = q;
  151. filp->f_pos++;
  152. }
  153. spin_unlock(&dcache_lock);
  154. }
  155. return 0;
  156. }
  157. ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
  158. {
  159. return -EISDIR;
  160. }
  161. const struct file_operations simple_dir_operations = {
  162. .open = dcache_dir_open,
  163. .release = dcache_dir_close,
  164. .llseek = dcache_dir_lseek,
  165. .read = generic_read_dir,
  166. .readdir = dcache_readdir,
  167. .fsync = simple_sync_file,
  168. };
  169. struct inode_operations simple_dir_inode_operations = {
  170. .lookup = simple_lookup,
  171. };
  172. /*
  173. * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
  174. * will never be mountable)
  175. */
  176. struct super_block *
  177. get_sb_pseudo(struct file_system_type *fs_type, char *name,
  178. struct super_operations *ops, unsigned long magic)
  179. {
  180. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  181. static struct super_operations default_ops = {.statfs = simple_statfs};
  182. struct dentry *dentry;
  183. struct inode *root;
  184. struct qstr d_name = {.name = name, .len = strlen(name)};
  185. if (IS_ERR(s))
  186. return s;
  187. s->s_flags = MS_NOUSER;
  188. s->s_maxbytes = ~0ULL;
  189. s->s_blocksize = 1024;
  190. s->s_blocksize_bits = 10;
  191. s->s_magic = magic;
  192. s->s_op = ops ? ops : &default_ops;
  193. s->s_time_gran = 1;
  194. root = new_inode(s);
  195. if (!root)
  196. goto Enomem;
  197. root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
  198. root->i_uid = root->i_gid = 0;
  199. root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
  200. dentry = d_alloc(NULL, &d_name);
  201. if (!dentry) {
  202. iput(root);
  203. goto Enomem;
  204. }
  205. dentry->d_sb = s;
  206. dentry->d_parent = dentry;
  207. d_instantiate(dentry, root);
  208. s->s_root = dentry;
  209. s->s_flags |= MS_ACTIVE;
  210. return s;
  211. Enomem:
  212. up_write(&s->s_umount);
  213. deactivate_super(s);
  214. return ERR_PTR(-ENOMEM);
  215. }
  216. int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  217. {
  218. struct inode *inode = old_dentry->d_inode;
  219. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  220. inode->i_nlink++;
  221. atomic_inc(&inode->i_count);
  222. dget(dentry);
  223. d_instantiate(dentry, inode);
  224. return 0;
  225. }
  226. static inline int simple_positive(struct dentry *dentry)
  227. {
  228. return dentry->d_inode && !d_unhashed(dentry);
  229. }
  230. int simple_empty(struct dentry *dentry)
  231. {
  232. struct dentry *child;
  233. int ret = 0;
  234. spin_lock(&dcache_lock);
  235. list_for_each_entry(child, &dentry->d_subdirs, d_u.d_child)
  236. if (simple_positive(child))
  237. goto out;
  238. ret = 1;
  239. out:
  240. spin_unlock(&dcache_lock);
  241. return ret;
  242. }
  243. int simple_unlink(struct inode *dir, struct dentry *dentry)
  244. {
  245. struct inode *inode = dentry->d_inode;
  246. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  247. inode->i_nlink--;
  248. dput(dentry);
  249. return 0;
  250. }
  251. int simple_rmdir(struct inode *dir, struct dentry *dentry)
  252. {
  253. if (!simple_empty(dentry))
  254. return -ENOTEMPTY;
  255. dentry->d_inode->i_nlink--;
  256. simple_unlink(dir, dentry);
  257. dir->i_nlink--;
  258. return 0;
  259. }
  260. int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
  261. struct inode *new_dir, struct dentry *new_dentry)
  262. {
  263. struct inode *inode = old_dentry->d_inode;
  264. int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
  265. if (!simple_empty(new_dentry))
  266. return -ENOTEMPTY;
  267. if (new_dentry->d_inode) {
  268. simple_unlink(new_dir, new_dentry);
  269. if (they_are_dirs)
  270. old_dir->i_nlink--;
  271. } else if (they_are_dirs) {
  272. old_dir->i_nlink--;
  273. new_dir->i_nlink++;
  274. }
  275. old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
  276. new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
  277. return 0;
  278. }
  279. int simple_readpage(struct file *file, struct page *page)
  280. {
  281. void *kaddr;
  282. if (PageUptodate(page))
  283. goto out;
  284. kaddr = kmap_atomic(page, KM_USER0);
  285. memset(kaddr, 0, PAGE_CACHE_SIZE);
  286. kunmap_atomic(kaddr, KM_USER0);
  287. flush_dcache_page(page);
  288. SetPageUptodate(page);
  289. out:
  290. unlock_page(page);
  291. return 0;
  292. }
  293. int simple_prepare_write(struct file *file, struct page *page,
  294. unsigned from, unsigned to)
  295. {
  296. if (!PageUptodate(page)) {
  297. if (to - from != PAGE_CACHE_SIZE) {
  298. void *kaddr = kmap_atomic(page, KM_USER0);
  299. memset(kaddr, 0, from);
  300. memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
  301. flush_dcache_page(page);
  302. kunmap_atomic(kaddr, KM_USER0);
  303. }
  304. SetPageUptodate(page);
  305. }
  306. return 0;
  307. }
  308. int simple_commit_write(struct file *file, struct page *page,
  309. unsigned offset, unsigned to)
  310. {
  311. struct inode *inode = page->mapping->host;
  312. loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
  313. /*
  314. * No need to use i_size_read() here, the i_size
  315. * cannot change under us because we hold the i_mutex.
  316. */
  317. if (pos > inode->i_size)
  318. i_size_write(inode, pos);
  319. set_page_dirty(page);
  320. return 0;
  321. }
  322. int simple_fill_super(struct super_block *s, int magic, struct tree_descr *files)
  323. {
  324. static struct super_operations s_ops = {.statfs = simple_statfs};
  325. struct inode *inode;
  326. struct dentry *root;
  327. struct dentry *dentry;
  328. int i;
  329. s->s_blocksize = PAGE_CACHE_SIZE;
  330. s->s_blocksize_bits = PAGE_CACHE_SHIFT;
  331. s->s_magic = magic;
  332. s->s_op = &s_ops;
  333. s->s_time_gran = 1;
  334. inode = new_inode(s);
  335. if (!inode)
  336. return -ENOMEM;
  337. inode->i_mode = S_IFDIR | 0755;
  338. inode->i_uid = inode->i_gid = 0;
  339. inode->i_blksize = PAGE_CACHE_SIZE;
  340. inode->i_blocks = 0;
  341. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  342. inode->i_op = &simple_dir_inode_operations;
  343. inode->i_fop = &simple_dir_operations;
  344. inode->i_nlink = 2;
  345. root = d_alloc_root(inode);
  346. if (!root) {
  347. iput(inode);
  348. return -ENOMEM;
  349. }
  350. for (i = 0; !files->name || files->name[0]; i++, files++) {
  351. if (!files->name)
  352. continue;
  353. dentry = d_alloc_name(root, files->name);
  354. if (!dentry)
  355. goto out;
  356. inode = new_inode(s);
  357. if (!inode)
  358. goto out;
  359. inode->i_mode = S_IFREG | files->mode;
  360. inode->i_uid = inode->i_gid = 0;
  361. inode->i_blksize = PAGE_CACHE_SIZE;
  362. inode->i_blocks = 0;
  363. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  364. inode->i_fop = files->ops;
  365. inode->i_ino = i;
  366. d_add(dentry, inode);
  367. }
  368. s->s_root = root;
  369. return 0;
  370. out:
  371. d_genocide(root);
  372. dput(root);
  373. return -ENOMEM;
  374. }
  375. static DEFINE_SPINLOCK(pin_fs_lock);
  376. int simple_pin_fs(char *name, struct vfsmount **mount, int *count)
  377. {
  378. struct vfsmount *mnt = NULL;
  379. spin_lock(&pin_fs_lock);
  380. if (unlikely(!*mount)) {
  381. spin_unlock(&pin_fs_lock);
  382. mnt = do_kern_mount(name, 0, name, NULL);
  383. if (IS_ERR(mnt))
  384. return PTR_ERR(mnt);
  385. spin_lock(&pin_fs_lock);
  386. if (!*mount)
  387. *mount = mnt;
  388. }
  389. mntget(*mount);
  390. ++*count;
  391. spin_unlock(&pin_fs_lock);
  392. mntput(mnt);
  393. return 0;
  394. }
  395. void simple_release_fs(struct vfsmount **mount, int *count)
  396. {
  397. struct vfsmount *mnt;
  398. spin_lock(&pin_fs_lock);
  399. mnt = *mount;
  400. if (!--*count)
  401. *mount = NULL;
  402. spin_unlock(&pin_fs_lock);
  403. mntput(mnt);
  404. }
  405. ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
  406. const void *from, size_t available)
  407. {
  408. loff_t pos = *ppos;
  409. if (pos < 0)
  410. return -EINVAL;
  411. if (pos >= available)
  412. return 0;
  413. if (count > available - pos)
  414. count = available - pos;
  415. if (copy_to_user(to, from + pos, count))
  416. return -EFAULT;
  417. *ppos = pos + count;
  418. return count;
  419. }
  420. /*
  421. * Transaction based IO.
  422. * The file expects a single write which triggers the transaction, and then
  423. * possibly a read which collects the result - which is stored in a
  424. * file-local buffer.
  425. */
  426. char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
  427. {
  428. struct simple_transaction_argresp *ar;
  429. static DEFINE_SPINLOCK(simple_transaction_lock);
  430. if (size > SIMPLE_TRANSACTION_LIMIT - 1)
  431. return ERR_PTR(-EFBIG);
  432. ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
  433. if (!ar)
  434. return ERR_PTR(-ENOMEM);
  435. spin_lock(&simple_transaction_lock);
  436. /* only one write allowed per open */
  437. if (file->private_data) {
  438. spin_unlock(&simple_transaction_lock);
  439. free_page((unsigned long)ar);
  440. return ERR_PTR(-EBUSY);
  441. }
  442. file->private_data = ar;
  443. spin_unlock(&simple_transaction_lock);
  444. if (copy_from_user(ar->data, buf, size))
  445. return ERR_PTR(-EFAULT);
  446. return ar->data;
  447. }
  448. ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  449. {
  450. struct simple_transaction_argresp *ar = file->private_data;
  451. if (!ar)
  452. return 0;
  453. return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
  454. }
  455. int simple_transaction_release(struct inode *inode, struct file *file)
  456. {
  457. free_page((unsigned long)file->private_data);
  458. return 0;
  459. }
  460. /* Simple attribute files */
  461. struct simple_attr {
  462. u64 (*get)(void *);
  463. void (*set)(void *, u64);
  464. char get_buf[24]; /* enough to store a u64 and "\n\0" */
  465. char set_buf[24];
  466. void *data;
  467. const char *fmt; /* format for read operation */
  468. struct mutex mutex; /* protects access to these buffers */
  469. };
  470. /* simple_attr_open is called by an actual attribute open file operation
  471. * to set the attribute specific access operations. */
  472. int simple_attr_open(struct inode *inode, struct file *file,
  473. u64 (*get)(void *), void (*set)(void *, u64),
  474. const char *fmt)
  475. {
  476. struct simple_attr *attr;
  477. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  478. if (!attr)
  479. return -ENOMEM;
  480. attr->get = get;
  481. attr->set = set;
  482. attr->data = inode->u.generic_ip;
  483. attr->fmt = fmt;
  484. mutex_init(&attr->mutex);
  485. file->private_data = attr;
  486. return nonseekable_open(inode, file);
  487. }
  488. int simple_attr_close(struct inode *inode, struct file *file)
  489. {
  490. kfree(file->private_data);
  491. return 0;
  492. }
  493. /* read from the buffer that is filled with the get function */
  494. ssize_t simple_attr_read(struct file *file, char __user *buf,
  495. size_t len, loff_t *ppos)
  496. {
  497. struct simple_attr *attr;
  498. size_t size;
  499. ssize_t ret;
  500. attr = file->private_data;
  501. if (!attr->get)
  502. return -EACCES;
  503. mutex_lock(&attr->mutex);
  504. if (*ppos) /* continued read */
  505. size = strlen(attr->get_buf);
  506. else /* first read */
  507. size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
  508. attr->fmt,
  509. (unsigned long long)attr->get(attr->data));
  510. ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
  511. mutex_unlock(&attr->mutex);
  512. return ret;
  513. }
  514. /* interpret the buffer as a number to call the set function with */
  515. ssize_t simple_attr_write(struct file *file, const char __user *buf,
  516. size_t len, loff_t *ppos)
  517. {
  518. struct simple_attr *attr;
  519. u64 val;
  520. size_t size;
  521. ssize_t ret;
  522. attr = file->private_data;
  523. if (!attr->set)
  524. return -EACCES;
  525. mutex_lock(&attr->mutex);
  526. ret = -EFAULT;
  527. size = min(sizeof(attr->set_buf) - 1, len);
  528. if (copy_from_user(attr->set_buf, buf, size))
  529. goto out;
  530. ret = len; /* claim we got the whole input */
  531. attr->set_buf[size] = '\0';
  532. val = simple_strtol(attr->set_buf, NULL, 0);
  533. attr->set(attr->data, val);
  534. out:
  535. mutex_unlock(&attr->mutex);
  536. return ret;
  537. }
  538. EXPORT_SYMBOL(dcache_dir_close);
  539. EXPORT_SYMBOL(dcache_dir_lseek);
  540. EXPORT_SYMBOL(dcache_dir_open);
  541. EXPORT_SYMBOL(dcache_readdir);
  542. EXPORT_SYMBOL(generic_read_dir);
  543. EXPORT_SYMBOL(get_sb_pseudo);
  544. EXPORT_SYMBOL(simple_commit_write);
  545. EXPORT_SYMBOL(simple_dir_inode_operations);
  546. EXPORT_SYMBOL(simple_dir_operations);
  547. EXPORT_SYMBOL(simple_empty);
  548. EXPORT_SYMBOL(d_alloc_name);
  549. EXPORT_SYMBOL(simple_fill_super);
  550. EXPORT_SYMBOL(simple_getattr);
  551. EXPORT_SYMBOL(simple_link);
  552. EXPORT_SYMBOL(simple_lookup);
  553. EXPORT_SYMBOL(simple_pin_fs);
  554. EXPORT_SYMBOL(simple_prepare_write);
  555. EXPORT_SYMBOL(simple_readpage);
  556. EXPORT_SYMBOL(simple_release_fs);
  557. EXPORT_SYMBOL(simple_rename);
  558. EXPORT_SYMBOL(simple_rmdir);
  559. EXPORT_SYMBOL(simple_statfs);
  560. EXPORT_SYMBOL(simple_sync_file);
  561. EXPORT_SYMBOL(simple_unlink);
  562. EXPORT_SYMBOL(simple_read_from_buffer);
  563. EXPORT_SYMBOL(simple_transaction_get);
  564. EXPORT_SYMBOL(simple_transaction_read);
  565. EXPORT_SYMBOL(simple_transaction_release);
  566. EXPORT_SYMBOL_GPL(simple_attr_open);
  567. EXPORT_SYMBOL_GPL(simple_attr_close);
  568. EXPORT_SYMBOL_GPL(simple_attr_read);
  569. EXPORT_SYMBOL_GPL(simple_attr_write);