inode.c 28 KB

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