inode.c 28 KB

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