inode.c 28 KB

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