dir.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2006 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/file.h>
  10. #include <linux/gfp.h>
  11. #include <linux/sched.h>
  12. #include <linux/namei.h>
  13. /*
  14. * FUSE caches dentries and attributes with separate timeout. The
  15. * time in jiffies until the dentry/attributes are valid is stored in
  16. * dentry->d_time and fuse_inode->i_time respectively.
  17. */
  18. /*
  19. * Calculate the time in jiffies until a dentry/attributes are valid
  20. */
  21. static unsigned long time_to_jiffies(unsigned long sec, unsigned long nsec)
  22. {
  23. if (sec || nsec) {
  24. struct timespec ts = {sec, nsec};
  25. return jiffies + timespec_to_jiffies(&ts);
  26. } else
  27. return jiffies - 1;
  28. }
  29. /*
  30. * Set dentry and possibly attribute timeouts from the lookup/mk*
  31. * replies
  32. */
  33. static void fuse_change_timeout(struct dentry *entry, struct fuse_entry_out *o)
  34. {
  35. entry->d_time = time_to_jiffies(o->entry_valid, o->entry_valid_nsec);
  36. if (entry->d_inode)
  37. get_fuse_inode(entry->d_inode)->i_time =
  38. time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
  39. }
  40. /*
  41. * Mark the attributes as stale, so that at the next call to
  42. * ->getattr() they will be fetched from userspace
  43. */
  44. void fuse_invalidate_attr(struct inode *inode)
  45. {
  46. get_fuse_inode(inode)->i_time = jiffies - 1;
  47. }
  48. /*
  49. * Just mark the entry as stale, so that a next attempt to look it up
  50. * will result in a new lookup call to userspace
  51. *
  52. * This is called when a dentry is about to become negative and the
  53. * timeout is unknown (unlink, rmdir, rename and in some cases
  54. * lookup)
  55. */
  56. static void fuse_invalidate_entry_cache(struct dentry *entry)
  57. {
  58. entry->d_time = jiffies - 1;
  59. }
  60. /*
  61. * Same as fuse_invalidate_entry_cache(), but also try to remove the
  62. * dentry from the hash
  63. */
  64. static void fuse_invalidate_entry(struct dentry *entry)
  65. {
  66. d_invalidate(entry);
  67. fuse_invalidate_entry_cache(entry);
  68. }
  69. static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
  70. struct dentry *entry,
  71. struct fuse_entry_out *outarg)
  72. {
  73. req->in.h.opcode = FUSE_LOOKUP;
  74. req->in.h.nodeid = get_node_id(dir);
  75. req->in.numargs = 1;
  76. req->in.args[0].size = entry->d_name.len + 1;
  77. req->in.args[0].value = entry->d_name.name;
  78. req->out.numargs = 1;
  79. req->out.args[0].size = sizeof(struct fuse_entry_out);
  80. req->out.args[0].value = outarg;
  81. }
  82. /*
  83. * Check whether the dentry is still valid
  84. *
  85. * If the entry validity timeout has expired and the dentry is
  86. * positive, try to redo the lookup. If the lookup results in a
  87. * different inode, then let the VFS invalidate the dentry and redo
  88. * the lookup once more. If the lookup results in the same inode,
  89. * then refresh the attributes, timeouts and mark the dentry valid.
  90. */
  91. static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
  92. {
  93. struct inode *inode = entry->d_inode;
  94. if (inode && is_bad_inode(inode))
  95. return 0;
  96. else if (time_after(jiffies, entry->d_time)) {
  97. int err;
  98. struct fuse_entry_out outarg;
  99. struct fuse_conn *fc;
  100. struct fuse_req *req;
  101. /* Doesn't hurt to "reset" the validity timeout */
  102. fuse_invalidate_entry_cache(entry);
  103. /* For negative dentries, always do a fresh lookup */
  104. if (!inode)
  105. return 0;
  106. fc = get_fuse_conn(inode);
  107. req = fuse_get_req(fc);
  108. if (IS_ERR(req))
  109. return 0;
  110. fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
  111. request_send(fc, req);
  112. err = req->out.h.error;
  113. /* Zero nodeid is same as -ENOENT */
  114. if (!err && !outarg.nodeid)
  115. err = -ENOENT;
  116. if (!err) {
  117. struct fuse_inode *fi = get_fuse_inode(inode);
  118. if (outarg.nodeid != get_node_id(inode)) {
  119. fuse_send_forget(fc, req, outarg.nodeid, 1);
  120. return 0;
  121. }
  122. fi->nlookup ++;
  123. }
  124. fuse_put_request(fc, req);
  125. if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
  126. return 0;
  127. fuse_change_attributes(inode, &outarg.attr);
  128. fuse_change_timeout(entry, &outarg);
  129. }
  130. return 1;
  131. }
  132. /*
  133. * Check if there's already a hashed alias of this directory inode.
  134. * If yes, then lookup and mkdir must not create a new alias.
  135. */
  136. static int dir_alias(struct inode *inode)
  137. {
  138. if (S_ISDIR(inode->i_mode)) {
  139. struct dentry *alias = d_find_alias(inode);
  140. if (alias) {
  141. dput(alias);
  142. return 1;
  143. }
  144. }
  145. return 0;
  146. }
  147. static int invalid_nodeid(u64 nodeid)
  148. {
  149. return !nodeid || nodeid == FUSE_ROOT_ID;
  150. }
  151. static struct dentry_operations fuse_dentry_operations = {
  152. .d_revalidate = fuse_dentry_revalidate,
  153. };
  154. static int valid_mode(int m)
  155. {
  156. return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
  157. S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
  158. }
  159. static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
  160. struct nameidata *nd)
  161. {
  162. int err;
  163. struct fuse_entry_out outarg;
  164. struct inode *inode = NULL;
  165. struct fuse_conn *fc = get_fuse_conn(dir);
  166. struct fuse_req *req;
  167. if (entry->d_name.len > FUSE_NAME_MAX)
  168. return ERR_PTR(-ENAMETOOLONG);
  169. req = fuse_get_req(fc);
  170. if (IS_ERR(req))
  171. return ERR_PTR(PTR_ERR(req));
  172. fuse_lookup_init(req, dir, entry, &outarg);
  173. request_send(fc, req);
  174. err = req->out.h.error;
  175. /* Zero nodeid is same as -ENOENT, but with valid timeout */
  176. if (!err && outarg.nodeid &&
  177. (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
  178. err = -EIO;
  179. if (!err && outarg.nodeid) {
  180. inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
  181. &outarg.attr);
  182. if (!inode) {
  183. fuse_send_forget(fc, req, outarg.nodeid, 1);
  184. return ERR_PTR(-ENOMEM);
  185. }
  186. }
  187. fuse_put_request(fc, req);
  188. if (err && err != -ENOENT)
  189. return ERR_PTR(err);
  190. if (inode && dir_alias(inode)) {
  191. iput(inode);
  192. return ERR_PTR(-EIO);
  193. }
  194. d_add(entry, inode);
  195. entry->d_op = &fuse_dentry_operations;
  196. if (!err)
  197. fuse_change_timeout(entry, &outarg);
  198. else
  199. fuse_invalidate_entry_cache(entry);
  200. return NULL;
  201. }
  202. /*
  203. * Synchronous release for the case when something goes wrong in CREATE_OPEN
  204. */
  205. static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
  206. u64 nodeid, int flags)
  207. {
  208. struct fuse_req *req;
  209. req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
  210. req->force = 1;
  211. request_send(fc, req);
  212. fuse_put_request(fc, req);
  213. }
  214. /*
  215. * Atomic create+open operation
  216. *
  217. * If the filesystem doesn't support this, then fall back to separate
  218. * 'mknod' + 'open' requests.
  219. */
  220. static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
  221. struct nameidata *nd)
  222. {
  223. int err;
  224. struct inode *inode;
  225. struct fuse_conn *fc = get_fuse_conn(dir);
  226. struct fuse_req *req;
  227. struct fuse_req *forget_req;
  228. struct fuse_open_in inarg;
  229. struct fuse_open_out outopen;
  230. struct fuse_entry_out outentry;
  231. struct fuse_file *ff;
  232. struct file *file;
  233. int flags = nd->intent.open.flags - 1;
  234. if (fc->no_create)
  235. return -ENOSYS;
  236. forget_req = fuse_get_req(fc);
  237. if (IS_ERR(forget_req))
  238. return PTR_ERR(forget_req);
  239. req = fuse_get_req(fc);
  240. err = PTR_ERR(req);
  241. if (IS_ERR(req))
  242. goto out_put_forget_req;
  243. err = -ENOMEM;
  244. ff = fuse_file_alloc();
  245. if (!ff)
  246. goto out_put_request;
  247. flags &= ~O_NOCTTY;
  248. memset(&inarg, 0, sizeof(inarg));
  249. inarg.flags = flags;
  250. inarg.mode = mode;
  251. req->in.h.opcode = FUSE_CREATE;
  252. req->in.h.nodeid = get_node_id(dir);
  253. req->in.numargs = 2;
  254. req->in.args[0].size = sizeof(inarg);
  255. req->in.args[0].value = &inarg;
  256. req->in.args[1].size = entry->d_name.len + 1;
  257. req->in.args[1].value = entry->d_name.name;
  258. req->out.numargs = 2;
  259. req->out.args[0].size = sizeof(outentry);
  260. req->out.args[0].value = &outentry;
  261. req->out.args[1].size = sizeof(outopen);
  262. req->out.args[1].value = &outopen;
  263. request_send(fc, req);
  264. err = req->out.h.error;
  265. if (err) {
  266. if (err == -ENOSYS)
  267. fc->no_create = 1;
  268. goto out_free_ff;
  269. }
  270. err = -EIO;
  271. if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
  272. goto out_free_ff;
  273. fuse_put_request(fc, req);
  274. inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
  275. &outentry.attr);
  276. if (!inode) {
  277. flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
  278. ff->fh = outopen.fh;
  279. fuse_sync_release(fc, ff, outentry.nodeid, flags);
  280. fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
  281. return -ENOMEM;
  282. }
  283. fuse_put_request(fc, forget_req);
  284. d_instantiate(entry, inode);
  285. fuse_change_timeout(entry, &outentry);
  286. file = lookup_instantiate_filp(nd, entry, generic_file_open);
  287. if (IS_ERR(file)) {
  288. ff->fh = outopen.fh;
  289. fuse_sync_release(fc, ff, outentry.nodeid, flags);
  290. return PTR_ERR(file);
  291. }
  292. fuse_finish_open(inode, file, ff, &outopen);
  293. return 0;
  294. out_free_ff:
  295. fuse_file_free(ff);
  296. out_put_request:
  297. fuse_put_request(fc, req);
  298. out_put_forget_req:
  299. fuse_put_request(fc, forget_req);
  300. return err;
  301. }
  302. /*
  303. * Code shared between mknod, mkdir, symlink and link
  304. */
  305. static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
  306. struct inode *dir, struct dentry *entry,
  307. int mode)
  308. {
  309. struct fuse_entry_out outarg;
  310. struct inode *inode;
  311. int err;
  312. req->in.h.nodeid = get_node_id(dir);
  313. req->out.numargs = 1;
  314. req->out.args[0].size = sizeof(outarg);
  315. req->out.args[0].value = &outarg;
  316. request_send(fc, req);
  317. err = req->out.h.error;
  318. if (err) {
  319. fuse_put_request(fc, req);
  320. return err;
  321. }
  322. err = -EIO;
  323. if (invalid_nodeid(outarg.nodeid))
  324. goto out_put_request;
  325. if ((outarg.attr.mode ^ mode) & S_IFMT)
  326. goto out_put_request;
  327. inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
  328. &outarg.attr);
  329. if (!inode) {
  330. fuse_send_forget(fc, req, outarg.nodeid, 1);
  331. return -ENOMEM;
  332. }
  333. fuse_put_request(fc, req);
  334. if (dir_alias(inode)) {
  335. iput(inode);
  336. return -EIO;
  337. }
  338. d_instantiate(entry, inode);
  339. fuse_change_timeout(entry, &outarg);
  340. fuse_invalidate_attr(dir);
  341. return 0;
  342. out_put_request:
  343. fuse_put_request(fc, req);
  344. return err;
  345. }
  346. static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
  347. dev_t rdev)
  348. {
  349. struct fuse_mknod_in inarg;
  350. struct fuse_conn *fc = get_fuse_conn(dir);
  351. struct fuse_req *req = fuse_get_req(fc);
  352. if (IS_ERR(req))
  353. return PTR_ERR(req);
  354. memset(&inarg, 0, sizeof(inarg));
  355. inarg.mode = mode;
  356. inarg.rdev = new_encode_dev(rdev);
  357. req->in.h.opcode = FUSE_MKNOD;
  358. req->in.numargs = 2;
  359. req->in.args[0].size = sizeof(inarg);
  360. req->in.args[0].value = &inarg;
  361. req->in.args[1].size = entry->d_name.len + 1;
  362. req->in.args[1].value = entry->d_name.name;
  363. return create_new_entry(fc, req, dir, entry, mode);
  364. }
  365. static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
  366. struct nameidata *nd)
  367. {
  368. if (nd && (nd->flags & LOOKUP_CREATE)) {
  369. int err = fuse_create_open(dir, entry, mode, nd);
  370. if (err != -ENOSYS)
  371. return err;
  372. /* Fall back on mknod */
  373. }
  374. return fuse_mknod(dir, entry, mode, 0);
  375. }
  376. static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
  377. {
  378. struct fuse_mkdir_in inarg;
  379. struct fuse_conn *fc = get_fuse_conn(dir);
  380. struct fuse_req *req = fuse_get_req(fc);
  381. if (IS_ERR(req))
  382. return PTR_ERR(req);
  383. memset(&inarg, 0, sizeof(inarg));
  384. inarg.mode = mode;
  385. req->in.h.opcode = FUSE_MKDIR;
  386. req->in.numargs = 2;
  387. req->in.args[0].size = sizeof(inarg);
  388. req->in.args[0].value = &inarg;
  389. req->in.args[1].size = entry->d_name.len + 1;
  390. req->in.args[1].value = entry->d_name.name;
  391. return create_new_entry(fc, req, dir, entry, S_IFDIR);
  392. }
  393. static int fuse_symlink(struct inode *dir, struct dentry *entry,
  394. const char *link)
  395. {
  396. struct fuse_conn *fc = get_fuse_conn(dir);
  397. unsigned len = strlen(link) + 1;
  398. struct fuse_req *req = fuse_get_req(fc);
  399. if (IS_ERR(req))
  400. return PTR_ERR(req);
  401. req->in.h.opcode = FUSE_SYMLINK;
  402. req->in.numargs = 2;
  403. req->in.args[0].size = entry->d_name.len + 1;
  404. req->in.args[0].value = entry->d_name.name;
  405. req->in.args[1].size = len;
  406. req->in.args[1].value = link;
  407. return create_new_entry(fc, req, dir, entry, S_IFLNK);
  408. }
  409. static int fuse_unlink(struct inode *dir, struct dentry *entry)
  410. {
  411. int err;
  412. struct fuse_conn *fc = get_fuse_conn(dir);
  413. struct fuse_req *req = fuse_get_req(fc);
  414. if (IS_ERR(req))
  415. return PTR_ERR(req);
  416. req->in.h.opcode = FUSE_UNLINK;
  417. req->in.h.nodeid = get_node_id(dir);
  418. req->in.numargs = 1;
  419. req->in.args[0].size = entry->d_name.len + 1;
  420. req->in.args[0].value = entry->d_name.name;
  421. request_send(fc, req);
  422. err = req->out.h.error;
  423. fuse_put_request(fc, req);
  424. if (!err) {
  425. struct inode *inode = entry->d_inode;
  426. /* Set nlink to zero so the inode can be cleared, if
  427. the inode does have more links this will be
  428. discovered at the next lookup/getattr */
  429. inode->i_nlink = 0;
  430. fuse_invalidate_attr(inode);
  431. fuse_invalidate_attr(dir);
  432. fuse_invalidate_entry_cache(entry);
  433. } else if (err == -EINTR)
  434. fuse_invalidate_entry(entry);
  435. return err;
  436. }
  437. static int fuse_rmdir(struct inode *dir, struct dentry *entry)
  438. {
  439. int err;
  440. struct fuse_conn *fc = get_fuse_conn(dir);
  441. struct fuse_req *req = fuse_get_req(fc);
  442. if (IS_ERR(req))
  443. return PTR_ERR(req);
  444. req->in.h.opcode = FUSE_RMDIR;
  445. req->in.h.nodeid = get_node_id(dir);
  446. req->in.numargs = 1;
  447. req->in.args[0].size = entry->d_name.len + 1;
  448. req->in.args[0].value = entry->d_name.name;
  449. request_send(fc, req);
  450. err = req->out.h.error;
  451. fuse_put_request(fc, req);
  452. if (!err) {
  453. entry->d_inode->i_nlink = 0;
  454. fuse_invalidate_attr(dir);
  455. fuse_invalidate_entry_cache(entry);
  456. } else if (err == -EINTR)
  457. fuse_invalidate_entry(entry);
  458. return err;
  459. }
  460. static int fuse_rename(struct inode *olddir, struct dentry *oldent,
  461. struct inode *newdir, struct dentry *newent)
  462. {
  463. int err;
  464. struct fuse_rename_in inarg;
  465. struct fuse_conn *fc = get_fuse_conn(olddir);
  466. struct fuse_req *req = fuse_get_req(fc);
  467. if (IS_ERR(req))
  468. return PTR_ERR(req);
  469. memset(&inarg, 0, sizeof(inarg));
  470. inarg.newdir = get_node_id(newdir);
  471. req->in.h.opcode = FUSE_RENAME;
  472. req->in.h.nodeid = get_node_id(olddir);
  473. req->in.numargs = 3;
  474. req->in.args[0].size = sizeof(inarg);
  475. req->in.args[0].value = &inarg;
  476. req->in.args[1].size = oldent->d_name.len + 1;
  477. req->in.args[1].value = oldent->d_name.name;
  478. req->in.args[2].size = newent->d_name.len + 1;
  479. req->in.args[2].value = newent->d_name.name;
  480. request_send(fc, req);
  481. err = req->out.h.error;
  482. fuse_put_request(fc, req);
  483. if (!err) {
  484. fuse_invalidate_attr(olddir);
  485. if (olddir != newdir)
  486. fuse_invalidate_attr(newdir);
  487. /* newent will end up negative */
  488. if (newent->d_inode)
  489. fuse_invalidate_entry_cache(newent);
  490. } else if (err == -EINTR) {
  491. /* If request was interrupted, DEITY only knows if the
  492. rename actually took place. If the invalidation
  493. fails (e.g. some process has CWD under the renamed
  494. directory), then there can be inconsistency between
  495. the dcache and the real filesystem. Tough luck. */
  496. fuse_invalidate_entry(oldent);
  497. if (newent->d_inode)
  498. fuse_invalidate_entry(newent);
  499. }
  500. return err;
  501. }
  502. static int fuse_link(struct dentry *entry, struct inode *newdir,
  503. struct dentry *newent)
  504. {
  505. int err;
  506. struct fuse_link_in inarg;
  507. struct inode *inode = entry->d_inode;
  508. struct fuse_conn *fc = get_fuse_conn(inode);
  509. struct fuse_req *req = fuse_get_req(fc);
  510. if (IS_ERR(req))
  511. return PTR_ERR(req);
  512. memset(&inarg, 0, sizeof(inarg));
  513. inarg.oldnodeid = get_node_id(inode);
  514. req->in.h.opcode = FUSE_LINK;
  515. req->in.numargs = 2;
  516. req->in.args[0].size = sizeof(inarg);
  517. req->in.args[0].value = &inarg;
  518. req->in.args[1].size = newent->d_name.len + 1;
  519. req->in.args[1].value = newent->d_name.name;
  520. err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
  521. /* Contrary to "normal" filesystems it can happen that link
  522. makes two "logical" inodes point to the same "physical"
  523. inode. We invalidate the attributes of the old one, so it
  524. will reflect changes in the backing inode (link count,
  525. etc.)
  526. */
  527. if (!err || err == -EINTR)
  528. fuse_invalidate_attr(inode);
  529. return err;
  530. }
  531. int fuse_do_getattr(struct inode *inode)
  532. {
  533. int err;
  534. struct fuse_attr_out arg;
  535. struct fuse_conn *fc = get_fuse_conn(inode);
  536. struct fuse_req *req = fuse_get_req(fc);
  537. if (IS_ERR(req))
  538. return PTR_ERR(req);
  539. req->in.h.opcode = FUSE_GETATTR;
  540. req->in.h.nodeid = get_node_id(inode);
  541. req->out.numargs = 1;
  542. req->out.args[0].size = sizeof(arg);
  543. req->out.args[0].value = &arg;
  544. request_send(fc, req);
  545. err = req->out.h.error;
  546. fuse_put_request(fc, req);
  547. if (!err) {
  548. if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
  549. make_bad_inode(inode);
  550. err = -EIO;
  551. } else {
  552. struct fuse_inode *fi = get_fuse_inode(inode);
  553. fuse_change_attributes(inode, &arg.attr);
  554. fi->i_time = time_to_jiffies(arg.attr_valid,
  555. arg.attr_valid_nsec);
  556. }
  557. }
  558. return err;
  559. }
  560. /*
  561. * Calling into a user-controlled filesystem gives the filesystem
  562. * daemon ptrace-like capabilities over the requester process. This
  563. * means, that the filesystem daemon is able to record the exact
  564. * filesystem operations performed, and can also control the behavior
  565. * of the requester process in otherwise impossible ways. For example
  566. * it can delay the operation for arbitrary length of time allowing
  567. * DoS against the requester.
  568. *
  569. * For this reason only those processes can call into the filesystem,
  570. * for which the owner of the mount has ptrace privilege. This
  571. * excludes processes started by other users, suid or sgid processes.
  572. */
  573. static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
  574. {
  575. if (fc->flags & FUSE_ALLOW_OTHER)
  576. return 1;
  577. if (task->euid == fc->user_id &&
  578. task->suid == fc->user_id &&
  579. task->uid == fc->user_id &&
  580. task->egid == fc->group_id &&
  581. task->sgid == fc->group_id &&
  582. task->gid == fc->group_id)
  583. return 1;
  584. return 0;
  585. }
  586. /*
  587. * Check whether the inode attributes are still valid
  588. *
  589. * If the attribute validity timeout has expired, then fetch the fresh
  590. * attributes with a 'getattr' request
  591. *
  592. * I'm not sure why cached attributes are never returned for the root
  593. * inode, this is probably being too cautious.
  594. */
  595. static int fuse_revalidate(struct dentry *entry)
  596. {
  597. struct inode *inode = entry->d_inode;
  598. struct fuse_inode *fi = get_fuse_inode(inode);
  599. struct fuse_conn *fc = get_fuse_conn(inode);
  600. if (!fuse_allow_task(fc, current))
  601. return -EACCES;
  602. if (get_node_id(inode) != FUSE_ROOT_ID &&
  603. time_before_eq(jiffies, fi->i_time))
  604. return 0;
  605. return fuse_do_getattr(inode);
  606. }
  607. static int fuse_access(struct inode *inode, int mask)
  608. {
  609. struct fuse_conn *fc = get_fuse_conn(inode);
  610. struct fuse_req *req;
  611. struct fuse_access_in inarg;
  612. int err;
  613. if (fc->no_access)
  614. return 0;
  615. req = fuse_get_req(fc);
  616. if (IS_ERR(req))
  617. return PTR_ERR(req);
  618. memset(&inarg, 0, sizeof(inarg));
  619. inarg.mask = mask;
  620. req->in.h.opcode = FUSE_ACCESS;
  621. req->in.h.nodeid = get_node_id(inode);
  622. req->in.numargs = 1;
  623. req->in.args[0].size = sizeof(inarg);
  624. req->in.args[0].value = &inarg;
  625. request_send(fc, req);
  626. err = req->out.h.error;
  627. fuse_put_request(fc, req);
  628. if (err == -ENOSYS) {
  629. fc->no_access = 1;
  630. err = 0;
  631. }
  632. return err;
  633. }
  634. /*
  635. * Check permission. The two basic access models of FUSE are:
  636. *
  637. * 1) Local access checking ('default_permissions' mount option) based
  638. * on file mode. This is the plain old disk filesystem permission
  639. * modell.
  640. *
  641. * 2) "Remote" access checking, where server is responsible for
  642. * checking permission in each inode operation. An exception to this
  643. * is if ->permission() was invoked from sys_access() in which case an
  644. * access request is sent. Execute permission is still checked
  645. * locally based on file mode.
  646. */
  647. static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
  648. {
  649. struct fuse_conn *fc = get_fuse_conn(inode);
  650. if (!fuse_allow_task(fc, current))
  651. return -EACCES;
  652. else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
  653. int err = generic_permission(inode, mask, NULL);
  654. /* If permission is denied, try to refresh file
  655. attributes. This is also needed, because the root
  656. node will at first have no permissions */
  657. if (err == -EACCES) {
  658. err = fuse_do_getattr(inode);
  659. if (!err)
  660. err = generic_permission(inode, mask, NULL);
  661. }
  662. /* Note: the opposite of the above test does not
  663. exist. So if permissions are revoked this won't be
  664. noticed immediately, only after the attribute
  665. timeout has expired */
  666. return err;
  667. } else {
  668. int mode = inode->i_mode;
  669. if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
  670. return -EACCES;
  671. if (nd && (nd->flags & LOOKUP_ACCESS))
  672. return fuse_access(inode, mask);
  673. return 0;
  674. }
  675. }
  676. static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
  677. void *dstbuf, filldir_t filldir)
  678. {
  679. while (nbytes >= FUSE_NAME_OFFSET) {
  680. struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
  681. size_t reclen = FUSE_DIRENT_SIZE(dirent);
  682. int over;
  683. if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
  684. return -EIO;
  685. if (reclen > nbytes)
  686. break;
  687. over = filldir(dstbuf, dirent->name, dirent->namelen,
  688. file->f_pos, dirent->ino, dirent->type);
  689. if (over)
  690. break;
  691. buf += reclen;
  692. nbytes -= reclen;
  693. file->f_pos = dirent->off;
  694. }
  695. return 0;
  696. }
  697. static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
  698. {
  699. int err;
  700. size_t nbytes;
  701. struct page *page;
  702. struct inode *inode = file->f_dentry->d_inode;
  703. struct fuse_conn *fc = get_fuse_conn(inode);
  704. struct fuse_req *req;
  705. if (is_bad_inode(inode))
  706. return -EIO;
  707. req = fuse_get_req(fc);
  708. if (IS_ERR(req))
  709. return PTR_ERR(req);
  710. page = alloc_page(GFP_KERNEL);
  711. if (!page) {
  712. fuse_put_request(fc, req);
  713. return -ENOMEM;
  714. }
  715. req->num_pages = 1;
  716. req->pages[0] = page;
  717. fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
  718. request_send(fc, req);
  719. nbytes = req->out.args[0].size;
  720. err = req->out.h.error;
  721. fuse_put_request(fc, req);
  722. if (!err)
  723. err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
  724. filldir);
  725. __free_page(page);
  726. fuse_invalidate_attr(inode); /* atime changed */
  727. return err;
  728. }
  729. static char *read_link(struct dentry *dentry)
  730. {
  731. struct inode *inode = dentry->d_inode;
  732. struct fuse_conn *fc = get_fuse_conn(inode);
  733. struct fuse_req *req = fuse_get_req(fc);
  734. char *link;
  735. if (IS_ERR(req))
  736. return ERR_PTR(PTR_ERR(req));
  737. link = (char *) __get_free_page(GFP_KERNEL);
  738. if (!link) {
  739. link = ERR_PTR(-ENOMEM);
  740. goto out;
  741. }
  742. req->in.h.opcode = FUSE_READLINK;
  743. req->in.h.nodeid = get_node_id(inode);
  744. req->out.argvar = 1;
  745. req->out.numargs = 1;
  746. req->out.args[0].size = PAGE_SIZE - 1;
  747. req->out.args[0].value = link;
  748. request_send(fc, req);
  749. if (req->out.h.error) {
  750. free_page((unsigned long) link);
  751. link = ERR_PTR(req->out.h.error);
  752. } else
  753. link[req->out.args[0].size] = '\0';
  754. out:
  755. fuse_put_request(fc, req);
  756. fuse_invalidate_attr(inode); /* atime changed */
  757. return link;
  758. }
  759. static void free_link(char *link)
  760. {
  761. if (!IS_ERR(link))
  762. free_page((unsigned long) link);
  763. }
  764. static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
  765. {
  766. nd_set_link(nd, read_link(dentry));
  767. return NULL;
  768. }
  769. static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
  770. {
  771. free_link(nd_get_link(nd));
  772. }
  773. static int fuse_dir_open(struct inode *inode, struct file *file)
  774. {
  775. return fuse_open_common(inode, file, 1);
  776. }
  777. static int fuse_dir_release(struct inode *inode, struct file *file)
  778. {
  779. return fuse_release_common(inode, file, 1);
  780. }
  781. static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
  782. {
  783. /* nfsd can call this with no file */
  784. return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
  785. }
  786. static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
  787. {
  788. unsigned ivalid = iattr->ia_valid;
  789. if (ivalid & ATTR_MODE)
  790. arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
  791. if (ivalid & ATTR_UID)
  792. arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
  793. if (ivalid & ATTR_GID)
  794. arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
  795. if (ivalid & ATTR_SIZE)
  796. arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
  797. /* You can only _set_ these together (they may change by themselves) */
  798. if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
  799. arg->valid |= FATTR_ATIME | FATTR_MTIME;
  800. arg->atime = iattr->ia_atime.tv_sec;
  801. arg->mtime = iattr->ia_mtime.tv_sec;
  802. }
  803. if (ivalid & ATTR_FILE) {
  804. struct fuse_file *ff = iattr->ia_file->private_data;
  805. arg->valid |= FATTR_FH;
  806. arg->fh = ff->fh;
  807. }
  808. }
  809. /*
  810. * Set attributes, and at the same time refresh them.
  811. *
  812. * Truncation is slightly complicated, because the 'truncate' request
  813. * may fail, in which case we don't want to touch the mapping.
  814. * vmtruncate() doesn't allow for this case. So do the rlimit
  815. * checking by hand and call vmtruncate() only after the file has
  816. * actually been truncated.
  817. */
  818. static int fuse_setattr(struct dentry *entry, struct iattr *attr)
  819. {
  820. struct inode *inode = entry->d_inode;
  821. struct fuse_conn *fc = get_fuse_conn(inode);
  822. struct fuse_inode *fi = get_fuse_inode(inode);
  823. struct fuse_req *req;
  824. struct fuse_setattr_in inarg;
  825. struct fuse_attr_out outarg;
  826. int err;
  827. int is_truncate = 0;
  828. if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
  829. err = inode_change_ok(inode, attr);
  830. if (err)
  831. return err;
  832. }
  833. if (attr->ia_valid & ATTR_SIZE) {
  834. unsigned long limit;
  835. is_truncate = 1;
  836. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  837. if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
  838. send_sig(SIGXFSZ, current, 0);
  839. return -EFBIG;
  840. }
  841. }
  842. req = fuse_get_req(fc);
  843. if (IS_ERR(req))
  844. return PTR_ERR(req);
  845. memset(&inarg, 0, sizeof(inarg));
  846. iattr_to_fattr(attr, &inarg);
  847. req->in.h.opcode = FUSE_SETATTR;
  848. req->in.h.nodeid = get_node_id(inode);
  849. req->in.numargs = 1;
  850. req->in.args[0].size = sizeof(inarg);
  851. req->in.args[0].value = &inarg;
  852. req->out.numargs = 1;
  853. req->out.args[0].size = sizeof(outarg);
  854. req->out.args[0].value = &outarg;
  855. request_send(fc, req);
  856. err = req->out.h.error;
  857. fuse_put_request(fc, req);
  858. if (!err) {
  859. if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
  860. make_bad_inode(inode);
  861. err = -EIO;
  862. } else {
  863. if (is_truncate) {
  864. loff_t origsize = i_size_read(inode);
  865. i_size_write(inode, outarg.attr.size);
  866. if (origsize > outarg.attr.size)
  867. vmtruncate(inode, outarg.attr.size);
  868. }
  869. fuse_change_attributes(inode, &outarg.attr);
  870. fi->i_time = time_to_jiffies(outarg.attr_valid,
  871. outarg.attr_valid_nsec);
  872. }
  873. } else if (err == -EINTR)
  874. fuse_invalidate_attr(inode);
  875. return err;
  876. }
  877. static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
  878. struct kstat *stat)
  879. {
  880. struct inode *inode = entry->d_inode;
  881. int err = fuse_revalidate(entry);
  882. if (!err)
  883. generic_fillattr(inode, stat);
  884. return err;
  885. }
  886. static int fuse_setxattr(struct dentry *entry, const char *name,
  887. const void *value, size_t size, int flags)
  888. {
  889. struct inode *inode = entry->d_inode;
  890. struct fuse_conn *fc = get_fuse_conn(inode);
  891. struct fuse_req *req;
  892. struct fuse_setxattr_in inarg;
  893. int err;
  894. if (fc->no_setxattr)
  895. return -EOPNOTSUPP;
  896. req = fuse_get_req(fc);
  897. if (IS_ERR(req))
  898. return PTR_ERR(req);
  899. memset(&inarg, 0, sizeof(inarg));
  900. inarg.size = size;
  901. inarg.flags = flags;
  902. req->in.h.opcode = FUSE_SETXATTR;
  903. req->in.h.nodeid = get_node_id(inode);
  904. req->in.numargs = 3;
  905. req->in.args[0].size = sizeof(inarg);
  906. req->in.args[0].value = &inarg;
  907. req->in.args[1].size = strlen(name) + 1;
  908. req->in.args[1].value = name;
  909. req->in.args[2].size = size;
  910. req->in.args[2].value = value;
  911. request_send(fc, req);
  912. err = req->out.h.error;
  913. fuse_put_request(fc, req);
  914. if (err == -ENOSYS) {
  915. fc->no_setxattr = 1;
  916. err = -EOPNOTSUPP;
  917. }
  918. return err;
  919. }
  920. static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
  921. void *value, size_t size)
  922. {
  923. struct inode *inode = entry->d_inode;
  924. struct fuse_conn *fc = get_fuse_conn(inode);
  925. struct fuse_req *req;
  926. struct fuse_getxattr_in inarg;
  927. struct fuse_getxattr_out outarg;
  928. ssize_t ret;
  929. if (fc->no_getxattr)
  930. return -EOPNOTSUPP;
  931. req = fuse_get_req(fc);
  932. if (IS_ERR(req))
  933. return PTR_ERR(req);
  934. memset(&inarg, 0, sizeof(inarg));
  935. inarg.size = size;
  936. req->in.h.opcode = FUSE_GETXATTR;
  937. req->in.h.nodeid = get_node_id(inode);
  938. req->in.numargs = 2;
  939. req->in.args[0].size = sizeof(inarg);
  940. req->in.args[0].value = &inarg;
  941. req->in.args[1].size = strlen(name) + 1;
  942. req->in.args[1].value = name;
  943. /* This is really two different operations rolled into one */
  944. req->out.numargs = 1;
  945. if (size) {
  946. req->out.argvar = 1;
  947. req->out.args[0].size = size;
  948. req->out.args[0].value = value;
  949. } else {
  950. req->out.args[0].size = sizeof(outarg);
  951. req->out.args[0].value = &outarg;
  952. }
  953. request_send(fc, req);
  954. ret = req->out.h.error;
  955. if (!ret)
  956. ret = size ? req->out.args[0].size : outarg.size;
  957. else {
  958. if (ret == -ENOSYS) {
  959. fc->no_getxattr = 1;
  960. ret = -EOPNOTSUPP;
  961. }
  962. }
  963. fuse_put_request(fc, req);
  964. return ret;
  965. }
  966. static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
  967. {
  968. struct inode *inode = entry->d_inode;
  969. struct fuse_conn *fc = get_fuse_conn(inode);
  970. struct fuse_req *req;
  971. struct fuse_getxattr_in inarg;
  972. struct fuse_getxattr_out outarg;
  973. ssize_t ret;
  974. if (fc->no_listxattr)
  975. return -EOPNOTSUPP;
  976. req = fuse_get_req(fc);
  977. if (IS_ERR(req))
  978. return PTR_ERR(req);
  979. memset(&inarg, 0, sizeof(inarg));
  980. inarg.size = size;
  981. req->in.h.opcode = FUSE_LISTXATTR;
  982. req->in.h.nodeid = get_node_id(inode);
  983. req->in.numargs = 1;
  984. req->in.args[0].size = sizeof(inarg);
  985. req->in.args[0].value = &inarg;
  986. /* This is really two different operations rolled into one */
  987. req->out.numargs = 1;
  988. if (size) {
  989. req->out.argvar = 1;
  990. req->out.args[0].size = size;
  991. req->out.args[0].value = list;
  992. } else {
  993. req->out.args[0].size = sizeof(outarg);
  994. req->out.args[0].value = &outarg;
  995. }
  996. request_send(fc, req);
  997. ret = req->out.h.error;
  998. if (!ret)
  999. ret = size ? req->out.args[0].size : outarg.size;
  1000. else {
  1001. if (ret == -ENOSYS) {
  1002. fc->no_listxattr = 1;
  1003. ret = -EOPNOTSUPP;
  1004. }
  1005. }
  1006. fuse_put_request(fc, req);
  1007. return ret;
  1008. }
  1009. static int fuse_removexattr(struct dentry *entry, const char *name)
  1010. {
  1011. struct inode *inode = entry->d_inode;
  1012. struct fuse_conn *fc = get_fuse_conn(inode);
  1013. struct fuse_req *req;
  1014. int err;
  1015. if (fc->no_removexattr)
  1016. return -EOPNOTSUPP;
  1017. req = fuse_get_req(fc);
  1018. if (IS_ERR(req))
  1019. return PTR_ERR(req);
  1020. req->in.h.opcode = FUSE_REMOVEXATTR;
  1021. req->in.h.nodeid = get_node_id(inode);
  1022. req->in.numargs = 1;
  1023. req->in.args[0].size = strlen(name) + 1;
  1024. req->in.args[0].value = name;
  1025. request_send(fc, req);
  1026. err = req->out.h.error;
  1027. fuse_put_request(fc, req);
  1028. if (err == -ENOSYS) {
  1029. fc->no_removexattr = 1;
  1030. err = -EOPNOTSUPP;
  1031. }
  1032. return err;
  1033. }
  1034. static struct inode_operations fuse_dir_inode_operations = {
  1035. .lookup = fuse_lookup,
  1036. .mkdir = fuse_mkdir,
  1037. .symlink = fuse_symlink,
  1038. .unlink = fuse_unlink,
  1039. .rmdir = fuse_rmdir,
  1040. .rename = fuse_rename,
  1041. .link = fuse_link,
  1042. .setattr = fuse_setattr,
  1043. .create = fuse_create,
  1044. .mknod = fuse_mknod,
  1045. .permission = fuse_permission,
  1046. .getattr = fuse_getattr,
  1047. .setxattr = fuse_setxattr,
  1048. .getxattr = fuse_getxattr,
  1049. .listxattr = fuse_listxattr,
  1050. .removexattr = fuse_removexattr,
  1051. };
  1052. static const struct file_operations fuse_dir_operations = {
  1053. .llseek = generic_file_llseek,
  1054. .read = generic_read_dir,
  1055. .readdir = fuse_readdir,
  1056. .open = fuse_dir_open,
  1057. .release = fuse_dir_release,
  1058. .fsync = fuse_dir_fsync,
  1059. };
  1060. static struct inode_operations fuse_common_inode_operations = {
  1061. .setattr = fuse_setattr,
  1062. .permission = fuse_permission,
  1063. .getattr = fuse_getattr,
  1064. .setxattr = fuse_setxattr,
  1065. .getxattr = fuse_getxattr,
  1066. .listxattr = fuse_listxattr,
  1067. .removexattr = fuse_removexattr,
  1068. };
  1069. static struct inode_operations fuse_symlink_inode_operations = {
  1070. .setattr = fuse_setattr,
  1071. .follow_link = fuse_follow_link,
  1072. .put_link = fuse_put_link,
  1073. .readlink = generic_readlink,
  1074. .getattr = fuse_getattr,
  1075. .setxattr = fuse_setxattr,
  1076. .getxattr = fuse_getxattr,
  1077. .listxattr = fuse_listxattr,
  1078. .removexattr = fuse_removexattr,
  1079. };
  1080. void fuse_init_common(struct inode *inode)
  1081. {
  1082. inode->i_op = &fuse_common_inode_operations;
  1083. }
  1084. void fuse_init_dir(struct inode *inode)
  1085. {
  1086. inode->i_op = &fuse_dir_inode_operations;
  1087. inode->i_fop = &fuse_dir_operations;
  1088. }
  1089. void fuse_init_symlink(struct inode *inode)
  1090. {
  1091. inode->i_op = &fuse_symlink_inode_operations;
  1092. }