libfs.c 15 KB

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