inode.c 28 KB

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