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