inode.c 29 KB

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