inode.c 25 KB

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