inode.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU GPL.
  5. See the file COPYING.
  6. */
  7. #include "fuse_i.h"
  8. #include <linux/pagemap.h>
  9. #include <linux/slab.h>
  10. #include <linux/file.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/parser.h>
  15. #include <linux/statfs.h>
  16. #include <linux/random.h>
  17. #include <linux/sched.h>
  18. #include <linux/exportfs.h>
  19. MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
  20. MODULE_DESCRIPTION("Filesystem in Userspace");
  21. MODULE_LICENSE("GPL");
  22. static struct kmem_cache *fuse_inode_cachep;
  23. struct list_head fuse_conn_list;
  24. DEFINE_MUTEX(fuse_mutex);
  25. #define FUSE_SUPER_MAGIC 0x65735546
  26. #define FUSE_DEFAULT_BLKSIZE 512
  27. struct fuse_mount_data {
  28. int fd;
  29. unsigned rootmode;
  30. unsigned user_id;
  31. unsigned group_id;
  32. unsigned fd_present:1;
  33. unsigned rootmode_present:1;
  34. unsigned user_id_present:1;
  35. unsigned group_id_present:1;
  36. unsigned flags;
  37. unsigned max_read;
  38. unsigned blksize;
  39. };
  40. static struct inode *fuse_alloc_inode(struct super_block *sb)
  41. {
  42. struct inode *inode;
  43. struct fuse_inode *fi;
  44. inode = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL);
  45. if (!inode)
  46. return NULL;
  47. fi = get_fuse_inode(inode);
  48. fi->i_time = 0;
  49. fi->nodeid = 0;
  50. fi->nlookup = 0;
  51. fi->attr_version = 0;
  52. fi->writectr = 0;
  53. INIT_LIST_HEAD(&fi->write_files);
  54. INIT_LIST_HEAD(&fi->queued_writes);
  55. INIT_LIST_HEAD(&fi->writepages);
  56. init_waitqueue_head(&fi->page_waitq);
  57. fi->forget_req = fuse_request_alloc();
  58. if (!fi->forget_req) {
  59. kmem_cache_free(fuse_inode_cachep, inode);
  60. return NULL;
  61. }
  62. return inode;
  63. }
  64. static void fuse_destroy_inode(struct inode *inode)
  65. {
  66. struct fuse_inode *fi = get_fuse_inode(inode);
  67. BUG_ON(!list_empty(&fi->write_files));
  68. BUG_ON(!list_empty(&fi->queued_writes));
  69. if (fi->forget_req)
  70. fuse_request_free(fi->forget_req);
  71. kmem_cache_free(fuse_inode_cachep, inode);
  72. }
  73. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  74. u64 nodeid, u64 nlookup)
  75. {
  76. struct fuse_forget_in *inarg = &req->misc.forget_in;
  77. inarg->nlookup = nlookup;
  78. req->in.h.opcode = FUSE_FORGET;
  79. req->in.h.nodeid = nodeid;
  80. req->in.numargs = 1;
  81. req->in.args[0].size = sizeof(struct fuse_forget_in);
  82. req->in.args[0].value = inarg;
  83. fuse_request_send_noreply(fc, req);
  84. }
  85. static void fuse_clear_inode(struct inode *inode)
  86. {
  87. if (inode->i_sb->s_flags & MS_ACTIVE) {
  88. struct fuse_conn *fc = get_fuse_conn(inode);
  89. struct fuse_inode *fi = get_fuse_inode(inode);
  90. fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
  91. fi->forget_req = NULL;
  92. }
  93. }
  94. static int fuse_remount_fs(struct super_block *sb, int *flags, char *data)
  95. {
  96. if (*flags & MS_MANDLOCK)
  97. return -EINVAL;
  98. return 0;
  99. }
  100. void fuse_truncate(struct address_space *mapping, loff_t offset)
  101. {
  102. /* See vmtruncate() */
  103. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  104. truncate_inode_pages(mapping, offset);
  105. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  106. }
  107. void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
  108. u64 attr_valid)
  109. {
  110. struct fuse_conn *fc = get_fuse_conn(inode);
  111. struct fuse_inode *fi = get_fuse_inode(inode);
  112. fi->attr_version = ++fc->attr_version;
  113. fi->i_time = attr_valid;
  114. inode->i_ino = attr->ino;
  115. inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
  116. inode->i_nlink = attr->nlink;
  117. inode->i_uid = attr->uid;
  118. inode->i_gid = attr->gid;
  119. inode->i_blocks = attr->blocks;
  120. inode->i_atime.tv_sec = attr->atime;
  121. inode->i_atime.tv_nsec = attr->atimensec;
  122. inode->i_mtime.tv_sec = attr->mtime;
  123. inode->i_mtime.tv_nsec = attr->mtimensec;
  124. inode->i_ctime.tv_sec = attr->ctime;
  125. inode->i_ctime.tv_nsec = attr->ctimensec;
  126. if (attr->blksize != 0)
  127. inode->i_blkbits = ilog2(attr->blksize);
  128. else
  129. inode->i_blkbits = inode->i_sb->s_blocksize_bits;
  130. /*
  131. * Don't set the sticky bit in i_mode, unless we want the VFS
  132. * to check permissions. This prevents failures due to the
  133. * check in may_delete().
  134. */
  135. fi->orig_i_mode = inode->i_mode;
  136. if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
  137. inode->i_mode &= ~S_ISVTX;
  138. }
  139. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
  140. u64 attr_valid, u64 attr_version)
  141. {
  142. struct fuse_conn *fc = get_fuse_conn(inode);
  143. struct fuse_inode *fi = get_fuse_inode(inode);
  144. loff_t oldsize;
  145. spin_lock(&fc->lock);
  146. if (attr_version != 0 && fi->attr_version > attr_version) {
  147. spin_unlock(&fc->lock);
  148. return;
  149. }
  150. fuse_change_attributes_common(inode, attr, attr_valid);
  151. oldsize = inode->i_size;
  152. i_size_write(inode, attr->size);
  153. spin_unlock(&fc->lock);
  154. if (S_ISREG(inode->i_mode) && oldsize != attr->size) {
  155. if (attr->size < oldsize)
  156. fuse_truncate(inode->i_mapping, attr->size);
  157. invalidate_inode_pages2(inode->i_mapping);
  158. }
  159. }
  160. static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
  161. {
  162. inode->i_mode = attr->mode & S_IFMT;
  163. inode->i_size = attr->size;
  164. if (S_ISREG(inode->i_mode)) {
  165. fuse_init_common(inode);
  166. fuse_init_file_inode(inode);
  167. } else if (S_ISDIR(inode->i_mode))
  168. fuse_init_dir(inode);
  169. else if (S_ISLNK(inode->i_mode))
  170. fuse_init_symlink(inode);
  171. else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  172. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  173. fuse_init_common(inode);
  174. init_special_inode(inode, inode->i_mode,
  175. new_decode_dev(attr->rdev));
  176. } else
  177. BUG();
  178. }
  179. static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
  180. {
  181. u64 nodeid = *(u64 *) _nodeidp;
  182. if (get_node_id(inode) == nodeid)
  183. return 1;
  184. else
  185. return 0;
  186. }
  187. static int fuse_inode_set(struct inode *inode, void *_nodeidp)
  188. {
  189. u64 nodeid = *(u64 *) _nodeidp;
  190. get_fuse_inode(inode)->nodeid = nodeid;
  191. return 0;
  192. }
  193. struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
  194. int generation, struct fuse_attr *attr,
  195. u64 attr_valid, u64 attr_version)
  196. {
  197. struct inode *inode;
  198. struct fuse_inode *fi;
  199. struct fuse_conn *fc = get_fuse_conn_super(sb);
  200. retry:
  201. inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
  202. if (!inode)
  203. return NULL;
  204. if ((inode->i_state & I_NEW)) {
  205. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  206. inode->i_generation = generation;
  207. inode->i_data.backing_dev_info = &fc->bdi;
  208. fuse_init_inode(inode, attr);
  209. unlock_new_inode(inode);
  210. } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
  211. /* Inode has changed type, any I/O on the old should fail */
  212. make_bad_inode(inode);
  213. iput(inode);
  214. goto retry;
  215. }
  216. fi = get_fuse_inode(inode);
  217. spin_lock(&fc->lock);
  218. fi->nlookup++;
  219. spin_unlock(&fc->lock);
  220. fuse_change_attributes(inode, attr, attr_valid, attr_version);
  221. return inode;
  222. }
  223. static void fuse_umount_begin(struct super_block *sb)
  224. {
  225. fuse_abort_conn(get_fuse_conn_super(sb));
  226. }
  227. static void fuse_send_destroy(struct fuse_conn *fc)
  228. {
  229. struct fuse_req *req = fc->destroy_req;
  230. if (req && fc->conn_init) {
  231. fc->destroy_req = NULL;
  232. req->in.h.opcode = FUSE_DESTROY;
  233. req->force = 1;
  234. fuse_request_send(fc, req);
  235. fuse_put_request(fc, req);
  236. }
  237. }
  238. static void fuse_bdi_destroy(struct fuse_conn *fc)
  239. {
  240. if (fc->bdi_initialized)
  241. bdi_destroy(&fc->bdi);
  242. }
  243. void fuse_conn_kill(struct fuse_conn *fc)
  244. {
  245. spin_lock(&fc->lock);
  246. fc->connected = 0;
  247. fc->blocked = 0;
  248. spin_unlock(&fc->lock);
  249. /* Flush all readers on this fs */
  250. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  251. wake_up_all(&fc->waitq);
  252. wake_up_all(&fc->blocked_waitq);
  253. wake_up_all(&fc->reserved_req_waitq);
  254. mutex_lock(&fuse_mutex);
  255. list_del(&fc->entry);
  256. fuse_ctl_remove_conn(fc);
  257. mutex_unlock(&fuse_mutex);
  258. fuse_bdi_destroy(fc);
  259. }
  260. EXPORT_SYMBOL_GPL(fuse_conn_kill);
  261. static void fuse_put_super(struct super_block *sb)
  262. {
  263. struct fuse_conn *fc = get_fuse_conn_super(sb);
  264. fuse_send_destroy(fc);
  265. fuse_conn_kill(fc);
  266. fuse_conn_put(fc);
  267. }
  268. static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
  269. {
  270. stbuf->f_type = FUSE_SUPER_MAGIC;
  271. stbuf->f_bsize = attr->bsize;
  272. stbuf->f_frsize = attr->frsize;
  273. stbuf->f_blocks = attr->blocks;
  274. stbuf->f_bfree = attr->bfree;
  275. stbuf->f_bavail = attr->bavail;
  276. stbuf->f_files = attr->files;
  277. stbuf->f_ffree = attr->ffree;
  278. stbuf->f_namelen = attr->namelen;
  279. /* fsid is left zero */
  280. }
  281. static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
  282. {
  283. struct super_block *sb = dentry->d_sb;
  284. struct fuse_conn *fc = get_fuse_conn_super(sb);
  285. struct fuse_req *req;
  286. struct fuse_statfs_out outarg;
  287. int err;
  288. if (!fuse_allow_task(fc, current)) {
  289. buf->f_type = FUSE_SUPER_MAGIC;
  290. return 0;
  291. }
  292. req = fuse_get_req(fc);
  293. if (IS_ERR(req))
  294. return PTR_ERR(req);
  295. memset(&outarg, 0, sizeof(outarg));
  296. req->in.numargs = 0;
  297. req->in.h.opcode = FUSE_STATFS;
  298. req->in.h.nodeid = get_node_id(dentry->d_inode);
  299. req->out.numargs = 1;
  300. req->out.args[0].size =
  301. fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
  302. req->out.args[0].value = &outarg;
  303. fuse_request_send(fc, req);
  304. err = req->out.h.error;
  305. if (!err)
  306. convert_fuse_statfs(buf, &outarg.st);
  307. fuse_put_request(fc, req);
  308. return err;
  309. }
  310. enum {
  311. OPT_FD,
  312. OPT_ROOTMODE,
  313. OPT_USER_ID,
  314. OPT_GROUP_ID,
  315. OPT_DEFAULT_PERMISSIONS,
  316. OPT_ALLOW_OTHER,
  317. OPT_MAX_READ,
  318. OPT_BLKSIZE,
  319. OPT_ERR
  320. };
  321. static const match_table_t tokens = {
  322. {OPT_FD, "fd=%u"},
  323. {OPT_ROOTMODE, "rootmode=%o"},
  324. {OPT_USER_ID, "user_id=%u"},
  325. {OPT_GROUP_ID, "group_id=%u"},
  326. {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
  327. {OPT_ALLOW_OTHER, "allow_other"},
  328. {OPT_MAX_READ, "max_read=%u"},
  329. {OPT_BLKSIZE, "blksize=%u"},
  330. {OPT_ERR, NULL}
  331. };
  332. static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev)
  333. {
  334. char *p;
  335. memset(d, 0, sizeof(struct fuse_mount_data));
  336. d->max_read = ~0;
  337. d->blksize = FUSE_DEFAULT_BLKSIZE;
  338. while ((p = strsep(&opt, ",")) != NULL) {
  339. int token;
  340. int value;
  341. substring_t args[MAX_OPT_ARGS];
  342. if (!*p)
  343. continue;
  344. token = match_token(p, tokens, args);
  345. switch (token) {
  346. case OPT_FD:
  347. if (match_int(&args[0], &value))
  348. return 0;
  349. d->fd = value;
  350. d->fd_present = 1;
  351. break;
  352. case OPT_ROOTMODE:
  353. if (match_octal(&args[0], &value))
  354. return 0;
  355. if (!fuse_valid_type(value))
  356. return 0;
  357. d->rootmode = value;
  358. d->rootmode_present = 1;
  359. break;
  360. case OPT_USER_ID:
  361. if (match_int(&args[0], &value))
  362. return 0;
  363. d->user_id = value;
  364. d->user_id_present = 1;
  365. break;
  366. case OPT_GROUP_ID:
  367. if (match_int(&args[0], &value))
  368. return 0;
  369. d->group_id = value;
  370. d->group_id_present = 1;
  371. break;
  372. case OPT_DEFAULT_PERMISSIONS:
  373. d->flags |= FUSE_DEFAULT_PERMISSIONS;
  374. break;
  375. case OPT_ALLOW_OTHER:
  376. d->flags |= FUSE_ALLOW_OTHER;
  377. break;
  378. case OPT_MAX_READ:
  379. if (match_int(&args[0], &value))
  380. return 0;
  381. d->max_read = value;
  382. break;
  383. case OPT_BLKSIZE:
  384. if (!is_bdev || match_int(&args[0], &value))
  385. return 0;
  386. d->blksize = value;
  387. break;
  388. default:
  389. return 0;
  390. }
  391. }
  392. if (!d->fd_present || !d->rootmode_present ||
  393. !d->user_id_present || !d->group_id_present)
  394. return 0;
  395. return 1;
  396. }
  397. static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
  398. {
  399. struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
  400. seq_printf(m, ",user_id=%u", fc->user_id);
  401. seq_printf(m, ",group_id=%u", fc->group_id);
  402. if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
  403. seq_puts(m, ",default_permissions");
  404. if (fc->flags & FUSE_ALLOW_OTHER)
  405. seq_puts(m, ",allow_other");
  406. if (fc->max_read != ~0)
  407. seq_printf(m, ",max_read=%u", fc->max_read);
  408. if (mnt->mnt_sb->s_bdev &&
  409. mnt->mnt_sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
  410. seq_printf(m, ",blksize=%lu", mnt->mnt_sb->s_blocksize);
  411. return 0;
  412. }
  413. void fuse_conn_init(struct fuse_conn *fc)
  414. {
  415. memset(fc, 0, sizeof(*fc));
  416. spin_lock_init(&fc->lock);
  417. mutex_init(&fc->inst_mutex);
  418. atomic_set(&fc->count, 1);
  419. init_waitqueue_head(&fc->waitq);
  420. init_waitqueue_head(&fc->blocked_waitq);
  421. init_waitqueue_head(&fc->reserved_req_waitq);
  422. INIT_LIST_HEAD(&fc->pending);
  423. INIT_LIST_HEAD(&fc->processing);
  424. INIT_LIST_HEAD(&fc->io);
  425. INIT_LIST_HEAD(&fc->interrupts);
  426. INIT_LIST_HEAD(&fc->bg_queue);
  427. INIT_LIST_HEAD(&fc->entry);
  428. atomic_set(&fc->num_waiting, 0);
  429. fc->khctr = 0;
  430. fc->polled_files = RB_ROOT;
  431. fc->reqctr = 0;
  432. fc->blocked = 1;
  433. fc->attr_version = 1;
  434. get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
  435. }
  436. EXPORT_SYMBOL_GPL(fuse_conn_init);
  437. void fuse_conn_put(struct fuse_conn *fc)
  438. {
  439. if (atomic_dec_and_test(&fc->count)) {
  440. if (fc->destroy_req)
  441. fuse_request_free(fc->destroy_req);
  442. mutex_destroy(&fc->inst_mutex);
  443. fc->release(fc);
  444. }
  445. }
  446. EXPORT_SYMBOL_GPL(fuse_conn_put);
  447. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
  448. {
  449. atomic_inc(&fc->count);
  450. return fc;
  451. }
  452. EXPORT_SYMBOL_GPL(fuse_conn_get);
  453. static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
  454. {
  455. struct fuse_attr attr;
  456. memset(&attr, 0, sizeof(attr));
  457. attr.mode = mode;
  458. attr.ino = FUSE_ROOT_ID;
  459. attr.nlink = 1;
  460. return fuse_iget(sb, 1, 0, &attr, 0, 0);
  461. }
  462. struct fuse_inode_handle {
  463. u64 nodeid;
  464. u32 generation;
  465. };
  466. static struct dentry *fuse_get_dentry(struct super_block *sb,
  467. struct fuse_inode_handle *handle)
  468. {
  469. struct fuse_conn *fc = get_fuse_conn_super(sb);
  470. struct inode *inode;
  471. struct dentry *entry;
  472. int err = -ESTALE;
  473. if (handle->nodeid == 0)
  474. goto out_err;
  475. inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
  476. if (!inode) {
  477. struct fuse_entry_out outarg;
  478. struct qstr name;
  479. if (!fc->export_support)
  480. goto out_err;
  481. name.len = 1;
  482. name.name = ".";
  483. err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
  484. &inode);
  485. if (err && err != -ENOENT)
  486. goto out_err;
  487. if (err || !inode) {
  488. err = -ESTALE;
  489. goto out_err;
  490. }
  491. err = -EIO;
  492. if (get_node_id(inode) != handle->nodeid)
  493. goto out_iput;
  494. }
  495. err = -ESTALE;
  496. if (inode->i_generation != handle->generation)
  497. goto out_iput;
  498. entry = d_obtain_alias(inode);
  499. if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID) {
  500. entry->d_op = &fuse_dentry_operations;
  501. fuse_invalidate_entry_cache(entry);
  502. }
  503. return entry;
  504. out_iput:
  505. iput(inode);
  506. out_err:
  507. return ERR_PTR(err);
  508. }
  509. static int fuse_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
  510. int connectable)
  511. {
  512. struct inode *inode = dentry->d_inode;
  513. bool encode_parent = connectable && !S_ISDIR(inode->i_mode);
  514. int len = encode_parent ? 6 : 3;
  515. u64 nodeid;
  516. u32 generation;
  517. if (*max_len < len)
  518. return 255;
  519. nodeid = get_fuse_inode(inode)->nodeid;
  520. generation = inode->i_generation;
  521. fh[0] = (u32)(nodeid >> 32);
  522. fh[1] = (u32)(nodeid & 0xffffffff);
  523. fh[2] = generation;
  524. if (encode_parent) {
  525. struct inode *parent;
  526. spin_lock(&dentry->d_lock);
  527. parent = dentry->d_parent->d_inode;
  528. nodeid = get_fuse_inode(parent)->nodeid;
  529. generation = parent->i_generation;
  530. spin_unlock(&dentry->d_lock);
  531. fh[3] = (u32)(nodeid >> 32);
  532. fh[4] = (u32)(nodeid & 0xffffffff);
  533. fh[5] = generation;
  534. }
  535. *max_len = len;
  536. return encode_parent ? 0x82 : 0x81;
  537. }
  538. static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
  539. struct fid *fid, int fh_len, int fh_type)
  540. {
  541. struct fuse_inode_handle handle;
  542. if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
  543. return NULL;
  544. handle.nodeid = (u64) fid->raw[0] << 32;
  545. handle.nodeid |= (u64) fid->raw[1];
  546. handle.generation = fid->raw[2];
  547. return fuse_get_dentry(sb, &handle);
  548. }
  549. static struct dentry *fuse_fh_to_parent(struct super_block *sb,
  550. struct fid *fid, int fh_len, int fh_type)
  551. {
  552. struct fuse_inode_handle parent;
  553. if (fh_type != 0x82 || fh_len < 6)
  554. return NULL;
  555. parent.nodeid = (u64) fid->raw[3] << 32;
  556. parent.nodeid |= (u64) fid->raw[4];
  557. parent.generation = fid->raw[5];
  558. return fuse_get_dentry(sb, &parent);
  559. }
  560. static struct dentry *fuse_get_parent(struct dentry *child)
  561. {
  562. struct inode *child_inode = child->d_inode;
  563. struct fuse_conn *fc = get_fuse_conn(child_inode);
  564. struct inode *inode;
  565. struct dentry *parent;
  566. struct fuse_entry_out outarg;
  567. struct qstr name;
  568. int err;
  569. if (!fc->export_support)
  570. return ERR_PTR(-ESTALE);
  571. name.len = 2;
  572. name.name = "..";
  573. err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
  574. &name, &outarg, &inode);
  575. if (err) {
  576. if (err == -ENOENT)
  577. return ERR_PTR(-ESTALE);
  578. return ERR_PTR(err);
  579. }
  580. parent = d_obtain_alias(inode);
  581. if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID) {
  582. parent->d_op = &fuse_dentry_operations;
  583. fuse_invalidate_entry_cache(parent);
  584. }
  585. return parent;
  586. }
  587. static const struct export_operations fuse_export_operations = {
  588. .fh_to_dentry = fuse_fh_to_dentry,
  589. .fh_to_parent = fuse_fh_to_parent,
  590. .encode_fh = fuse_encode_fh,
  591. .get_parent = fuse_get_parent,
  592. };
  593. static const struct super_operations fuse_super_operations = {
  594. .alloc_inode = fuse_alloc_inode,
  595. .destroy_inode = fuse_destroy_inode,
  596. .clear_inode = fuse_clear_inode,
  597. .drop_inode = generic_delete_inode,
  598. .remount_fs = fuse_remount_fs,
  599. .put_super = fuse_put_super,
  600. .umount_begin = fuse_umount_begin,
  601. .statfs = fuse_statfs,
  602. .show_options = fuse_show_options,
  603. };
  604. static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
  605. {
  606. struct fuse_init_out *arg = &req->misc.init_out;
  607. if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
  608. fc->conn_error = 1;
  609. else {
  610. unsigned long ra_pages;
  611. if (arg->minor >= 6) {
  612. ra_pages = arg->max_readahead / PAGE_CACHE_SIZE;
  613. if (arg->flags & FUSE_ASYNC_READ)
  614. fc->async_read = 1;
  615. if (!(arg->flags & FUSE_POSIX_LOCKS))
  616. fc->no_lock = 1;
  617. if (arg->flags & FUSE_ATOMIC_O_TRUNC)
  618. fc->atomic_o_trunc = 1;
  619. if (arg->minor >= 9) {
  620. /* LOOKUP has dependency on proto version */
  621. if (arg->flags & FUSE_EXPORT_SUPPORT)
  622. fc->export_support = 1;
  623. }
  624. if (arg->flags & FUSE_BIG_WRITES)
  625. fc->big_writes = 1;
  626. } else {
  627. ra_pages = fc->max_read / PAGE_CACHE_SIZE;
  628. fc->no_lock = 1;
  629. }
  630. fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
  631. fc->minor = arg->minor;
  632. fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
  633. fc->max_write = max_t(unsigned, 4096, fc->max_write);
  634. fc->conn_init = 1;
  635. }
  636. fc->blocked = 0;
  637. wake_up_all(&fc->blocked_waitq);
  638. }
  639. static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
  640. {
  641. struct fuse_init_in *arg = &req->misc.init_in;
  642. arg->major = FUSE_KERNEL_VERSION;
  643. arg->minor = FUSE_KERNEL_MINOR_VERSION;
  644. arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
  645. arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
  646. FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES;
  647. req->in.h.opcode = FUSE_INIT;
  648. req->in.numargs = 1;
  649. req->in.args[0].size = sizeof(*arg);
  650. req->in.args[0].value = arg;
  651. req->out.numargs = 1;
  652. /* Variable length arguement used for backward compatibility
  653. with interface version < 7.5. Rest of init_out is zeroed
  654. by do_get_request(), so a short reply is not a problem */
  655. req->out.argvar = 1;
  656. req->out.args[0].size = sizeof(struct fuse_init_out);
  657. req->out.args[0].value = &req->misc.init_out;
  658. req->end = process_init_reply;
  659. fuse_request_send_background(fc, req);
  660. }
  661. static void fuse_free_conn(struct fuse_conn *fc)
  662. {
  663. kfree(fc);
  664. }
  665. static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
  666. {
  667. int err;
  668. fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  669. fc->bdi.unplug_io_fn = default_unplug_io_fn;
  670. /* fuse does it's own writeback accounting */
  671. fc->bdi.capabilities = BDI_CAP_NO_ACCT_WB;
  672. err = bdi_init(&fc->bdi);
  673. if (err)
  674. return err;
  675. fc->bdi_initialized = 1;
  676. if (sb->s_bdev) {
  677. err = bdi_register(&fc->bdi, NULL, "%u:%u-fuseblk",
  678. MAJOR(fc->dev), MINOR(fc->dev));
  679. } else {
  680. err = bdi_register_dev(&fc->bdi, fc->dev);
  681. }
  682. if (err)
  683. return err;
  684. /*
  685. * For a single fuse filesystem use max 1% of dirty +
  686. * writeback threshold.
  687. *
  688. * This gives about 1M of write buffer for memory maps on a
  689. * machine with 1G and 10% dirty_ratio, which should be more
  690. * than enough.
  691. *
  692. * Privileged users can raise it by writing to
  693. *
  694. * /sys/class/bdi/<bdi>/max_ratio
  695. */
  696. bdi_set_max_ratio(&fc->bdi, 1);
  697. return 0;
  698. }
  699. static int fuse_fill_super(struct super_block *sb, void *data, int silent)
  700. {
  701. struct fuse_conn *fc;
  702. struct inode *root;
  703. struct fuse_mount_data d;
  704. struct file *file;
  705. struct dentry *root_dentry;
  706. struct fuse_req *init_req;
  707. int err;
  708. int is_bdev = sb->s_bdev != NULL;
  709. err = -EINVAL;
  710. if (sb->s_flags & MS_MANDLOCK)
  711. goto err;
  712. if (!parse_fuse_opt((char *) data, &d, is_bdev))
  713. goto err;
  714. if (is_bdev) {
  715. #ifdef CONFIG_BLOCK
  716. err = -EINVAL;
  717. if (!sb_set_blocksize(sb, d.blksize))
  718. goto err;
  719. #endif
  720. } else {
  721. sb->s_blocksize = PAGE_CACHE_SIZE;
  722. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  723. }
  724. sb->s_magic = FUSE_SUPER_MAGIC;
  725. sb->s_op = &fuse_super_operations;
  726. sb->s_maxbytes = MAX_LFS_FILESIZE;
  727. sb->s_export_op = &fuse_export_operations;
  728. file = fget(d.fd);
  729. err = -EINVAL;
  730. if (!file)
  731. goto err;
  732. if (file->f_op != &fuse_dev_operations)
  733. goto err_fput;
  734. fc = kmalloc(sizeof(*fc), GFP_KERNEL);
  735. err = -ENOMEM;
  736. if (!fc)
  737. goto err_fput;
  738. fuse_conn_init(fc);
  739. fc->dev = sb->s_dev;
  740. err = fuse_bdi_init(fc, sb);
  741. if (err)
  742. goto err_put_conn;
  743. fc->release = fuse_free_conn;
  744. fc->flags = d.flags;
  745. fc->user_id = d.user_id;
  746. fc->group_id = d.group_id;
  747. fc->max_read = max_t(unsigned, 4096, d.max_read);
  748. /* Used by get_root_inode() */
  749. sb->s_fs_info = fc;
  750. err = -ENOMEM;
  751. root = fuse_get_root_inode(sb, d.rootmode);
  752. if (!root)
  753. goto err_put_conn;
  754. root_dentry = d_alloc_root(root);
  755. if (!root_dentry) {
  756. iput(root);
  757. goto err_put_conn;
  758. }
  759. init_req = fuse_request_alloc();
  760. if (!init_req)
  761. goto err_put_root;
  762. if (is_bdev) {
  763. fc->destroy_req = fuse_request_alloc();
  764. if (!fc->destroy_req)
  765. goto err_free_init_req;
  766. }
  767. mutex_lock(&fuse_mutex);
  768. err = -EINVAL;
  769. if (file->private_data)
  770. goto err_unlock;
  771. err = fuse_ctl_add_conn(fc);
  772. if (err)
  773. goto err_unlock;
  774. list_add_tail(&fc->entry, &fuse_conn_list);
  775. sb->s_root = root_dentry;
  776. fc->connected = 1;
  777. file->private_data = fuse_conn_get(fc);
  778. mutex_unlock(&fuse_mutex);
  779. /*
  780. * atomic_dec_and_test() in fput() provides the necessary
  781. * memory barrier for file->private_data to be visible on all
  782. * CPUs after this
  783. */
  784. fput(file);
  785. fuse_send_init(fc, init_req);
  786. return 0;
  787. err_unlock:
  788. mutex_unlock(&fuse_mutex);
  789. err_free_init_req:
  790. fuse_request_free(init_req);
  791. err_put_root:
  792. dput(root_dentry);
  793. err_put_conn:
  794. fuse_bdi_destroy(fc);
  795. fuse_conn_put(fc);
  796. err_fput:
  797. fput(file);
  798. err:
  799. return err;
  800. }
  801. static int fuse_get_sb(struct file_system_type *fs_type,
  802. int flags, const char *dev_name,
  803. void *raw_data, struct vfsmount *mnt)
  804. {
  805. return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt);
  806. }
  807. static struct file_system_type fuse_fs_type = {
  808. .owner = THIS_MODULE,
  809. .name = "fuse",
  810. .fs_flags = FS_HAS_SUBTYPE,
  811. .get_sb = fuse_get_sb,
  812. .kill_sb = kill_anon_super,
  813. };
  814. #ifdef CONFIG_BLOCK
  815. static int fuse_get_sb_blk(struct file_system_type *fs_type,
  816. int flags, const char *dev_name,
  817. void *raw_data, struct vfsmount *mnt)
  818. {
  819. return get_sb_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super,
  820. mnt);
  821. }
  822. static struct file_system_type fuseblk_fs_type = {
  823. .owner = THIS_MODULE,
  824. .name = "fuseblk",
  825. .get_sb = fuse_get_sb_blk,
  826. .kill_sb = kill_block_super,
  827. .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
  828. };
  829. static inline int register_fuseblk(void)
  830. {
  831. return register_filesystem(&fuseblk_fs_type);
  832. }
  833. static inline void unregister_fuseblk(void)
  834. {
  835. unregister_filesystem(&fuseblk_fs_type);
  836. }
  837. #else
  838. static inline int register_fuseblk(void)
  839. {
  840. return 0;
  841. }
  842. static inline void unregister_fuseblk(void)
  843. {
  844. }
  845. #endif
  846. static void fuse_inode_init_once(void *foo)
  847. {
  848. struct inode *inode = foo;
  849. inode_init_once(inode);
  850. }
  851. static int __init fuse_fs_init(void)
  852. {
  853. int err;
  854. err = register_filesystem(&fuse_fs_type);
  855. if (err)
  856. goto out;
  857. err = register_fuseblk();
  858. if (err)
  859. goto out_unreg;
  860. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  861. sizeof(struct fuse_inode),
  862. 0, SLAB_HWCACHE_ALIGN,
  863. fuse_inode_init_once);
  864. err = -ENOMEM;
  865. if (!fuse_inode_cachep)
  866. goto out_unreg2;
  867. return 0;
  868. out_unreg2:
  869. unregister_fuseblk();
  870. out_unreg:
  871. unregister_filesystem(&fuse_fs_type);
  872. out:
  873. return err;
  874. }
  875. static void fuse_fs_cleanup(void)
  876. {
  877. unregister_filesystem(&fuse_fs_type);
  878. unregister_fuseblk();
  879. kmem_cache_destroy(fuse_inode_cachep);
  880. }
  881. static struct kobject *fuse_kobj;
  882. static struct kobject *connections_kobj;
  883. static int fuse_sysfs_init(void)
  884. {
  885. int err;
  886. fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
  887. if (!fuse_kobj) {
  888. err = -ENOMEM;
  889. goto out_err;
  890. }
  891. connections_kobj = kobject_create_and_add("connections", fuse_kobj);
  892. if (!connections_kobj) {
  893. err = -ENOMEM;
  894. goto out_fuse_unregister;
  895. }
  896. return 0;
  897. out_fuse_unregister:
  898. kobject_put(fuse_kobj);
  899. out_err:
  900. return err;
  901. }
  902. static void fuse_sysfs_cleanup(void)
  903. {
  904. kobject_put(connections_kobj);
  905. kobject_put(fuse_kobj);
  906. }
  907. static int __init fuse_init(void)
  908. {
  909. int res;
  910. printk(KERN_INFO "fuse init (API version %i.%i)\n",
  911. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  912. INIT_LIST_HEAD(&fuse_conn_list);
  913. res = fuse_fs_init();
  914. if (res)
  915. goto err;
  916. res = fuse_dev_init();
  917. if (res)
  918. goto err_fs_cleanup;
  919. res = fuse_sysfs_init();
  920. if (res)
  921. goto err_dev_cleanup;
  922. res = fuse_ctl_init();
  923. if (res)
  924. goto err_sysfs_cleanup;
  925. return 0;
  926. err_sysfs_cleanup:
  927. fuse_sysfs_cleanup();
  928. err_dev_cleanup:
  929. fuse_dev_cleanup();
  930. err_fs_cleanup:
  931. fuse_fs_cleanup();
  932. err:
  933. return res;
  934. }
  935. static void __exit fuse_exit(void)
  936. {
  937. printk(KERN_DEBUG "fuse exit\n");
  938. fuse_ctl_cleanup();
  939. fuse_sysfs_cleanup();
  940. fuse_fs_cleanup();
  941. fuse_dev_cleanup();
  942. }
  943. module_init(fuse_init);
  944. module_exit(fuse_exit);