vfs_inode.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  1. /*
  2. * linux/fs/9p/vfs_inode.c
  3. *
  4. * This file contains vfs inode ops for the 9P2000 protocol.
  5. *
  6. * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
  7. * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to:
  20. * Free Software Foundation
  21. * 51 Franklin Street, Fifth Floor
  22. * Boston, MA 02111-1301 USA
  23. *
  24. */
  25. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/module.h>
  27. #include <linux/errno.h>
  28. #include <linux/fs.h>
  29. #include <linux/file.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/stat.h>
  32. #include <linux/string.h>
  33. #include <linux/inet.h>
  34. #include <linux/namei.h>
  35. #include <linux/idr.h>
  36. #include <linux/sched.h>
  37. #include <linux/slab.h>
  38. #include <linux/xattr.h>
  39. #include <linux/posix_acl.h>
  40. #include <net/9p/9p.h>
  41. #include <net/9p/client.h>
  42. #include "v9fs.h"
  43. #include "v9fs_vfs.h"
  44. #include "fid.h"
  45. #include "cache.h"
  46. #include "xattr.h"
  47. #include "acl.h"
  48. static const struct inode_operations v9fs_dir_inode_operations;
  49. static const struct inode_operations v9fs_dir_inode_operations_dotu;
  50. static const struct inode_operations v9fs_file_inode_operations;
  51. static const struct inode_operations v9fs_symlink_inode_operations;
  52. /**
  53. * unixmode2p9mode - convert unix mode bits to plan 9
  54. * @v9ses: v9fs session information
  55. * @mode: mode to convert
  56. *
  57. */
  58. static u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode)
  59. {
  60. int res;
  61. res = mode & 0777;
  62. if (S_ISDIR(mode))
  63. res |= P9_DMDIR;
  64. if (v9fs_proto_dotu(v9ses)) {
  65. if (v9ses->nodev == 0) {
  66. if (S_ISSOCK(mode))
  67. res |= P9_DMSOCKET;
  68. if (S_ISFIFO(mode))
  69. res |= P9_DMNAMEDPIPE;
  70. if (S_ISBLK(mode))
  71. res |= P9_DMDEVICE;
  72. if (S_ISCHR(mode))
  73. res |= P9_DMDEVICE;
  74. }
  75. if ((mode & S_ISUID) == S_ISUID)
  76. res |= P9_DMSETUID;
  77. if ((mode & S_ISGID) == S_ISGID)
  78. res |= P9_DMSETGID;
  79. if ((mode & S_ISVTX) == S_ISVTX)
  80. res |= P9_DMSETVTX;
  81. }
  82. return res;
  83. }
  84. /**
  85. * p9mode2perm- convert plan9 mode bits to unix permission bits
  86. * @v9ses: v9fs session information
  87. * @stat: p9_wstat from which mode need to be derived
  88. *
  89. */
  90. static int p9mode2perm(struct v9fs_session_info *v9ses,
  91. struct p9_wstat *stat)
  92. {
  93. int res;
  94. int mode = stat->mode;
  95. res = mode & S_IALLUGO;
  96. if (v9fs_proto_dotu(v9ses)) {
  97. if ((mode & P9_DMSETUID) == P9_DMSETUID)
  98. res |= S_ISUID;
  99. if ((mode & P9_DMSETGID) == P9_DMSETGID)
  100. res |= S_ISGID;
  101. if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
  102. res |= S_ISVTX;
  103. }
  104. return res;
  105. }
  106. /**
  107. * p9mode2unixmode- convert plan9 mode bits to unix mode bits
  108. * @v9ses: v9fs session information
  109. * @stat: p9_wstat from which mode need to be derived
  110. * @rdev: major number, minor number in case of device files.
  111. *
  112. */
  113. static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses,
  114. struct p9_wstat *stat, dev_t *rdev)
  115. {
  116. int res;
  117. u32 mode = stat->mode;
  118. *rdev = 0;
  119. res = p9mode2perm(v9ses, stat);
  120. if ((mode & P9_DMDIR) == P9_DMDIR)
  121. res |= S_IFDIR;
  122. else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
  123. res |= S_IFLNK;
  124. else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
  125. && (v9ses->nodev == 0))
  126. res |= S_IFSOCK;
  127. else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
  128. && (v9ses->nodev == 0))
  129. res |= S_IFIFO;
  130. else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
  131. && (v9ses->nodev == 0)) {
  132. char type = 0, ext[32];
  133. int major = -1, minor = -1;
  134. strlcpy(ext, stat->extension, sizeof(ext));
  135. sscanf(ext, "%c %i %i", &type, &major, &minor);
  136. switch (type) {
  137. case 'c':
  138. res |= S_IFCHR;
  139. break;
  140. case 'b':
  141. res |= S_IFBLK;
  142. break;
  143. default:
  144. p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n",
  145. type, stat->extension);
  146. };
  147. *rdev = MKDEV(major, minor);
  148. } else
  149. res |= S_IFREG;
  150. return res;
  151. }
  152. /**
  153. * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
  154. * @uflags: flags to convert
  155. * @extended: if .u extensions are active
  156. */
  157. int v9fs_uflags2omode(int uflags, int extended)
  158. {
  159. int ret;
  160. ret = 0;
  161. switch (uflags&3) {
  162. default:
  163. case O_RDONLY:
  164. ret = P9_OREAD;
  165. break;
  166. case O_WRONLY:
  167. ret = P9_OWRITE;
  168. break;
  169. case O_RDWR:
  170. ret = P9_ORDWR;
  171. break;
  172. }
  173. if (extended) {
  174. if (uflags & O_EXCL)
  175. ret |= P9_OEXCL;
  176. if (uflags & O_APPEND)
  177. ret |= P9_OAPPEND;
  178. }
  179. return ret;
  180. }
  181. /**
  182. * v9fs_blank_wstat - helper function to setup a 9P stat structure
  183. * @wstat: structure to initialize
  184. *
  185. */
  186. void
  187. v9fs_blank_wstat(struct p9_wstat *wstat)
  188. {
  189. wstat->type = ~0;
  190. wstat->dev = ~0;
  191. wstat->qid.type = ~0;
  192. wstat->qid.version = ~0;
  193. *((long long *)&wstat->qid.path) = ~0;
  194. wstat->mode = ~0;
  195. wstat->atime = ~0;
  196. wstat->mtime = ~0;
  197. wstat->length = ~0;
  198. wstat->name = NULL;
  199. wstat->uid = NULL;
  200. wstat->gid = NULL;
  201. wstat->muid = NULL;
  202. wstat->n_uid = INVALID_UID;
  203. wstat->n_gid = INVALID_GID;
  204. wstat->n_muid = INVALID_UID;
  205. wstat->extension = NULL;
  206. }
  207. /**
  208. * v9fs_alloc_inode - helper function to allocate an inode
  209. *
  210. */
  211. struct inode *v9fs_alloc_inode(struct super_block *sb)
  212. {
  213. struct v9fs_inode *v9inode;
  214. v9inode = (struct v9fs_inode *)kmem_cache_alloc(v9fs_inode_cache,
  215. GFP_KERNEL);
  216. if (!v9inode)
  217. return NULL;
  218. #ifdef CONFIG_9P_FSCACHE
  219. v9inode->fscache = NULL;
  220. mutex_init(&v9inode->fscache_lock);
  221. #endif
  222. v9inode->writeback_fid = NULL;
  223. v9inode->cache_validity = 0;
  224. mutex_init(&v9inode->v_mutex);
  225. return &v9inode->vfs_inode;
  226. }
  227. /**
  228. * v9fs_destroy_inode - destroy an inode
  229. *
  230. */
  231. static void v9fs_i_callback(struct rcu_head *head)
  232. {
  233. struct inode *inode = container_of(head, struct inode, i_rcu);
  234. kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
  235. }
  236. void v9fs_destroy_inode(struct inode *inode)
  237. {
  238. call_rcu(&inode->i_rcu, v9fs_i_callback);
  239. }
  240. int v9fs_init_inode(struct v9fs_session_info *v9ses,
  241. struct inode *inode, umode_t mode, dev_t rdev)
  242. {
  243. int err = 0;
  244. inode_init_owner(inode, NULL, mode);
  245. inode->i_blocks = 0;
  246. inode->i_rdev = rdev;
  247. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  248. inode->i_mapping->a_ops = &v9fs_addr_operations;
  249. switch (mode & S_IFMT) {
  250. case S_IFIFO:
  251. case S_IFBLK:
  252. case S_IFCHR:
  253. case S_IFSOCK:
  254. if (v9fs_proto_dotl(v9ses)) {
  255. inode->i_op = &v9fs_file_inode_operations_dotl;
  256. } else if (v9fs_proto_dotu(v9ses)) {
  257. inode->i_op = &v9fs_file_inode_operations;
  258. } else {
  259. p9_debug(P9_DEBUG_ERROR,
  260. "special files without extended mode\n");
  261. err = -EINVAL;
  262. goto error;
  263. }
  264. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  265. break;
  266. case S_IFREG:
  267. if (v9fs_proto_dotl(v9ses)) {
  268. inode->i_op = &v9fs_file_inode_operations_dotl;
  269. if (v9ses->cache == CACHE_LOOSE ||
  270. v9ses->cache == CACHE_FSCACHE)
  271. inode->i_fop =
  272. &v9fs_cached_file_operations_dotl;
  273. else if (v9ses->cache == CACHE_MMAP)
  274. inode->i_fop = &v9fs_mmap_file_operations_dotl;
  275. else
  276. inode->i_fop = &v9fs_file_operations_dotl;
  277. } else {
  278. inode->i_op = &v9fs_file_inode_operations;
  279. if (v9ses->cache == CACHE_LOOSE ||
  280. v9ses->cache == CACHE_FSCACHE)
  281. inode->i_fop =
  282. &v9fs_cached_file_operations;
  283. else if (v9ses->cache == CACHE_MMAP)
  284. inode->i_fop = &v9fs_mmap_file_operations;
  285. else
  286. inode->i_fop = &v9fs_file_operations;
  287. }
  288. break;
  289. case S_IFLNK:
  290. if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
  291. p9_debug(P9_DEBUG_ERROR,
  292. "extended modes used with legacy protocol\n");
  293. err = -EINVAL;
  294. goto error;
  295. }
  296. if (v9fs_proto_dotl(v9ses))
  297. inode->i_op = &v9fs_symlink_inode_operations_dotl;
  298. else
  299. inode->i_op = &v9fs_symlink_inode_operations;
  300. break;
  301. case S_IFDIR:
  302. inc_nlink(inode);
  303. if (v9fs_proto_dotl(v9ses))
  304. inode->i_op = &v9fs_dir_inode_operations_dotl;
  305. else if (v9fs_proto_dotu(v9ses))
  306. inode->i_op = &v9fs_dir_inode_operations_dotu;
  307. else
  308. inode->i_op = &v9fs_dir_inode_operations;
  309. if (v9fs_proto_dotl(v9ses))
  310. inode->i_fop = &v9fs_dir_operations_dotl;
  311. else
  312. inode->i_fop = &v9fs_dir_operations;
  313. break;
  314. default:
  315. p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
  316. mode, mode & S_IFMT);
  317. err = -EINVAL;
  318. goto error;
  319. }
  320. error:
  321. return err;
  322. }
  323. /**
  324. * v9fs_get_inode - helper function to setup an inode
  325. * @sb: superblock
  326. * @mode: mode to setup inode with
  327. *
  328. */
  329. struct inode *v9fs_get_inode(struct super_block *sb, umode_t mode, dev_t rdev)
  330. {
  331. int err;
  332. struct inode *inode;
  333. struct v9fs_session_info *v9ses = sb->s_fs_info;
  334. p9_debug(P9_DEBUG_VFS, "super block: %p mode: %ho\n", sb, mode);
  335. inode = new_inode(sb);
  336. if (!inode) {
  337. pr_warn("%s (%d): Problem allocating inode\n",
  338. __func__, task_pid_nr(current));
  339. return ERR_PTR(-ENOMEM);
  340. }
  341. err = v9fs_init_inode(v9ses, inode, mode, rdev);
  342. if (err) {
  343. iput(inode);
  344. return ERR_PTR(err);
  345. }
  346. return inode;
  347. }
  348. /*
  349. static struct v9fs_fid*
  350. v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
  351. {
  352. int err;
  353. int nfid;
  354. struct v9fs_fid *ret;
  355. struct v9fs_fcall *fcall;
  356. nfid = v9fs_get_idpool(&v9ses->fidpool);
  357. if (nfid < 0) {
  358. eprintk(KERN_WARNING, "no free fids available\n");
  359. return ERR_PTR(-ENOSPC);
  360. }
  361. err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
  362. &fcall);
  363. if (err < 0) {
  364. if (fcall && fcall->id == RWALK)
  365. goto clunk_fid;
  366. PRINT_FCALL_ERROR("walk error", fcall);
  367. v9fs_put_idpool(nfid, &v9ses->fidpool);
  368. goto error;
  369. }
  370. kfree(fcall);
  371. fcall = NULL;
  372. ret = v9fs_fid_create(v9ses, nfid);
  373. if (!ret) {
  374. err = -ENOMEM;
  375. goto clunk_fid;
  376. }
  377. err = v9fs_fid_insert(ret, dentry);
  378. if (err < 0) {
  379. v9fs_fid_destroy(ret);
  380. goto clunk_fid;
  381. }
  382. return ret;
  383. clunk_fid:
  384. v9fs_t_clunk(v9ses, nfid);
  385. error:
  386. kfree(fcall);
  387. return ERR_PTR(err);
  388. }
  389. */
  390. /**
  391. * v9fs_clear_inode - release an inode
  392. * @inode: inode to release
  393. *
  394. */
  395. void v9fs_evict_inode(struct inode *inode)
  396. {
  397. struct v9fs_inode *v9inode = V9FS_I(inode);
  398. truncate_inode_pages_final(&inode->i_data);
  399. clear_inode(inode);
  400. filemap_fdatawrite(&inode->i_data);
  401. v9fs_cache_inode_put_cookie(inode);
  402. /* clunk the fid stashed in writeback_fid */
  403. if (v9inode->writeback_fid) {
  404. p9_client_clunk(v9inode->writeback_fid);
  405. v9inode->writeback_fid = NULL;
  406. }
  407. }
  408. static int v9fs_test_inode(struct inode *inode, void *data)
  409. {
  410. int umode;
  411. dev_t rdev;
  412. struct v9fs_inode *v9inode = V9FS_I(inode);
  413. struct p9_wstat *st = (struct p9_wstat *)data;
  414. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  415. umode = p9mode2unixmode(v9ses, st, &rdev);
  416. /* don't match inode of different type */
  417. if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
  418. return 0;
  419. /* compare qid details */
  420. if (memcmp(&v9inode->qid.version,
  421. &st->qid.version, sizeof(v9inode->qid.version)))
  422. return 0;
  423. if (v9inode->qid.type != st->qid.type)
  424. return 0;
  425. if (v9inode->qid.path != st->qid.path)
  426. return 0;
  427. return 1;
  428. }
  429. static int v9fs_test_new_inode(struct inode *inode, void *data)
  430. {
  431. return 0;
  432. }
  433. static int v9fs_set_inode(struct inode *inode, void *data)
  434. {
  435. struct v9fs_inode *v9inode = V9FS_I(inode);
  436. struct p9_wstat *st = (struct p9_wstat *)data;
  437. memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
  438. return 0;
  439. }
  440. static struct inode *v9fs_qid_iget(struct super_block *sb,
  441. struct p9_qid *qid,
  442. struct p9_wstat *st,
  443. int new)
  444. {
  445. dev_t rdev;
  446. int retval;
  447. umode_t umode;
  448. unsigned long i_ino;
  449. struct inode *inode;
  450. struct v9fs_session_info *v9ses = sb->s_fs_info;
  451. int (*test)(struct inode *, void *);
  452. if (new)
  453. test = v9fs_test_new_inode;
  454. else
  455. test = v9fs_test_inode;
  456. i_ino = v9fs_qid2ino(qid);
  457. inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
  458. if (!inode)
  459. return ERR_PTR(-ENOMEM);
  460. if (!(inode->i_state & I_NEW))
  461. return inode;
  462. /*
  463. * initialize the inode with the stat info
  464. * FIXME!! we may need support for stale inodes
  465. * later.
  466. */
  467. inode->i_ino = i_ino;
  468. umode = p9mode2unixmode(v9ses, st, &rdev);
  469. retval = v9fs_init_inode(v9ses, inode, umode, rdev);
  470. if (retval)
  471. goto error;
  472. v9fs_stat2inode(st, inode, sb, 0);
  473. v9fs_cache_inode_get_cookie(inode);
  474. unlock_new_inode(inode);
  475. return inode;
  476. error:
  477. iget_failed(inode);
  478. return ERR_PTR(retval);
  479. }
  480. struct inode *
  481. v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
  482. struct super_block *sb, int new)
  483. {
  484. struct p9_wstat *st;
  485. struct inode *inode = NULL;
  486. st = p9_client_stat(fid);
  487. if (IS_ERR(st))
  488. return ERR_CAST(st);
  489. inode = v9fs_qid_iget(sb, &st->qid, st, new);
  490. p9stat_free(st);
  491. kfree(st);
  492. return inode;
  493. }
  494. /**
  495. * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
  496. * plan 9 AT flag.
  497. * @flags: flags to convert
  498. */
  499. static int v9fs_at_to_dotl_flags(int flags)
  500. {
  501. int rflags = 0;
  502. if (flags & AT_REMOVEDIR)
  503. rflags |= P9_DOTL_AT_REMOVEDIR;
  504. return rflags;
  505. }
  506. /**
  507. * v9fs_dec_count - helper functon to drop i_nlink.
  508. *
  509. * If a directory had nlink <= 2 (including . and ..), then we should not drop
  510. * the link count, which indicates the underlying exported fs doesn't maintain
  511. * nlink accurately. e.g.
  512. * - overlayfs sets nlink to 1 for merged dir
  513. * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more
  514. * than EXT4_LINK_MAX (65000) links.
  515. *
  516. * @inode: inode whose nlink is being dropped
  517. */
  518. static void v9fs_dec_count(struct inode *inode)
  519. {
  520. if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
  521. drop_nlink(inode);
  522. }
  523. /**
  524. * v9fs_remove - helper function to remove files and directories
  525. * @dir: directory inode that is being deleted
  526. * @dentry: dentry that is being deleted
  527. * @flags: removing a directory
  528. *
  529. */
  530. static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
  531. {
  532. struct inode *inode;
  533. int retval = -EOPNOTSUPP;
  534. struct p9_fid *v9fid, *dfid;
  535. struct v9fs_session_info *v9ses;
  536. p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
  537. dir, dentry, flags);
  538. v9ses = v9fs_inode2v9ses(dir);
  539. inode = d_inode(dentry);
  540. dfid = v9fs_parent_fid(dentry);
  541. if (IS_ERR(dfid)) {
  542. retval = PTR_ERR(dfid);
  543. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
  544. return retval;
  545. }
  546. if (v9fs_proto_dotl(v9ses))
  547. retval = p9_client_unlinkat(dfid, dentry->d_name.name,
  548. v9fs_at_to_dotl_flags(flags));
  549. if (retval == -EOPNOTSUPP) {
  550. /* Try the one based on path */
  551. v9fid = v9fs_fid_clone(dentry);
  552. if (IS_ERR(v9fid))
  553. return PTR_ERR(v9fid);
  554. retval = p9_client_remove(v9fid);
  555. }
  556. if (!retval) {
  557. /*
  558. * directories on unlink should have zero
  559. * link count
  560. */
  561. if (flags & AT_REMOVEDIR) {
  562. clear_nlink(inode);
  563. v9fs_dec_count(dir);
  564. } else
  565. v9fs_dec_count(inode);
  566. v9fs_invalidate_inode_attr(inode);
  567. v9fs_invalidate_inode_attr(dir);
  568. }
  569. return retval;
  570. }
  571. /**
  572. * v9fs_create - Create a file
  573. * @v9ses: session information
  574. * @dir: directory that dentry is being created in
  575. * @dentry: dentry that is being created
  576. * @extension: 9p2000.u extension string to support devices, etc.
  577. * @perm: create permissions
  578. * @mode: open mode
  579. *
  580. */
  581. static struct p9_fid *
  582. v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
  583. struct dentry *dentry, char *extension, u32 perm, u8 mode)
  584. {
  585. int err;
  586. const unsigned char *name;
  587. struct p9_fid *dfid, *ofid, *fid;
  588. struct inode *inode;
  589. p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
  590. err = 0;
  591. ofid = NULL;
  592. fid = NULL;
  593. name = dentry->d_name.name;
  594. dfid = v9fs_parent_fid(dentry);
  595. if (IS_ERR(dfid)) {
  596. err = PTR_ERR(dfid);
  597. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  598. return ERR_PTR(err);
  599. }
  600. /* clone a fid to use for creation */
  601. ofid = clone_fid(dfid);
  602. if (IS_ERR(ofid)) {
  603. err = PTR_ERR(ofid);
  604. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  605. return ERR_PTR(err);
  606. }
  607. err = p9_client_fcreate(ofid, name, perm, mode, extension);
  608. if (err < 0) {
  609. p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
  610. goto error;
  611. }
  612. if (!(perm & P9_DMLINK)) {
  613. /* now walk from the parent so we can get unopened fid */
  614. fid = p9_client_walk(dfid, 1, &name, 1);
  615. if (IS_ERR(fid)) {
  616. err = PTR_ERR(fid);
  617. p9_debug(P9_DEBUG_VFS,
  618. "p9_client_walk failed %d\n", err);
  619. fid = NULL;
  620. goto error;
  621. }
  622. /*
  623. * instantiate inode and assign the unopened fid to the dentry
  624. */
  625. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  626. if (IS_ERR(inode)) {
  627. err = PTR_ERR(inode);
  628. p9_debug(P9_DEBUG_VFS,
  629. "inode creation failed %d\n", err);
  630. goto error;
  631. }
  632. v9fs_fid_add(dentry, fid);
  633. d_instantiate(dentry, inode);
  634. }
  635. return ofid;
  636. error:
  637. if (ofid)
  638. p9_client_clunk(ofid);
  639. if (fid)
  640. p9_client_clunk(fid);
  641. return ERR_PTR(err);
  642. }
  643. /**
  644. * v9fs_vfs_create - VFS hook to create a regular file
  645. *
  646. * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open(). This is only called
  647. * for mknod(2).
  648. *
  649. * @dir: directory inode that is being created
  650. * @dentry: dentry that is being deleted
  651. * @mode: create permissions
  652. *
  653. */
  654. static int
  655. v9fs_vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  656. bool excl)
  657. {
  658. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
  659. u32 perm = unixmode2p9mode(v9ses, mode);
  660. struct p9_fid *fid;
  661. /* P9_OEXCL? */
  662. fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
  663. if (IS_ERR(fid))
  664. return PTR_ERR(fid);
  665. v9fs_invalidate_inode_attr(dir);
  666. p9_client_clunk(fid);
  667. return 0;
  668. }
  669. /**
  670. * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
  671. * @dir: inode that is being unlinked
  672. * @dentry: dentry that is being unlinked
  673. * @mode: mode for new directory
  674. *
  675. */
  676. static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  677. {
  678. int err;
  679. u32 perm;
  680. struct p9_fid *fid;
  681. struct v9fs_session_info *v9ses;
  682. p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
  683. err = 0;
  684. v9ses = v9fs_inode2v9ses(dir);
  685. perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
  686. fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
  687. if (IS_ERR(fid)) {
  688. err = PTR_ERR(fid);
  689. fid = NULL;
  690. } else {
  691. inc_nlink(dir);
  692. v9fs_invalidate_inode_attr(dir);
  693. }
  694. if (fid)
  695. p9_client_clunk(fid);
  696. return err;
  697. }
  698. /**
  699. * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
  700. * @dir: inode that is being walked from
  701. * @dentry: dentry that is being walked to?
  702. * @flags: lookup flags (unused)
  703. *
  704. */
  705. struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
  706. unsigned int flags)
  707. {
  708. struct dentry *res;
  709. struct v9fs_session_info *v9ses;
  710. struct p9_fid *dfid, *fid;
  711. struct inode *inode;
  712. const unsigned char *name;
  713. p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
  714. dir, dentry, dentry, flags);
  715. if (dentry->d_name.len > NAME_MAX)
  716. return ERR_PTR(-ENAMETOOLONG);
  717. v9ses = v9fs_inode2v9ses(dir);
  718. /* We can walk d_parent because we hold the dir->i_mutex */
  719. dfid = v9fs_parent_fid(dentry);
  720. if (IS_ERR(dfid))
  721. return ERR_CAST(dfid);
  722. /*
  723. * Make sure we don't use a wrong inode due to parallel
  724. * unlink. For cached mode create calls request for new
  725. * inode. But with cache disabled, lookup should do this.
  726. */
  727. name = dentry->d_name.name;
  728. fid = p9_client_walk(dfid, 1, &name, 1);
  729. if (fid == ERR_PTR(-ENOENT))
  730. inode = NULL;
  731. else if (IS_ERR(fid))
  732. inode = ERR_CAST(fid);
  733. else if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  734. inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
  735. else
  736. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  737. /*
  738. * If we had a rename on the server and a parallel lookup
  739. * for the new name, then make sure we instantiate with
  740. * the new name. ie look up for a/b, while on server somebody
  741. * moved b under k and client parallely did a lookup for
  742. * k/b.
  743. */
  744. res = d_splice_alias(inode, dentry);
  745. if (!IS_ERR(fid)) {
  746. if (!res)
  747. v9fs_fid_add(dentry, fid);
  748. else if (!IS_ERR(res))
  749. v9fs_fid_add(res, fid);
  750. else
  751. p9_client_clunk(fid);
  752. }
  753. return res;
  754. }
  755. static int
  756. v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
  757. struct file *file, unsigned flags, umode_t mode)
  758. {
  759. int err;
  760. u32 perm;
  761. struct v9fs_inode *v9inode;
  762. struct v9fs_session_info *v9ses;
  763. struct p9_fid *fid, *inode_fid;
  764. struct dentry *res = NULL;
  765. if (d_in_lookup(dentry)) {
  766. res = v9fs_vfs_lookup(dir, dentry, 0);
  767. if (IS_ERR(res))
  768. return PTR_ERR(res);
  769. if (res)
  770. dentry = res;
  771. }
  772. /* Only creates */
  773. if (!(flags & O_CREAT) || d_really_is_positive(dentry))
  774. return finish_no_open(file, res);
  775. err = 0;
  776. v9ses = v9fs_inode2v9ses(dir);
  777. perm = unixmode2p9mode(v9ses, mode);
  778. fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
  779. v9fs_uflags2omode(flags,
  780. v9fs_proto_dotu(v9ses)));
  781. if (IS_ERR(fid)) {
  782. err = PTR_ERR(fid);
  783. fid = NULL;
  784. goto error;
  785. }
  786. v9fs_invalidate_inode_attr(dir);
  787. v9inode = V9FS_I(d_inode(dentry));
  788. mutex_lock(&v9inode->v_mutex);
  789. if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
  790. !v9inode->writeback_fid &&
  791. ((flags & O_ACCMODE) != O_RDONLY)) {
  792. /*
  793. * clone a fid and add it to writeback_fid
  794. * we do it during open time instead of
  795. * page dirty time via write_begin/page_mkwrite
  796. * because we want write after unlink usecase
  797. * to work.
  798. */
  799. inode_fid = v9fs_writeback_fid(dentry);
  800. if (IS_ERR(inode_fid)) {
  801. err = PTR_ERR(inode_fid);
  802. mutex_unlock(&v9inode->v_mutex);
  803. goto error;
  804. }
  805. v9inode->writeback_fid = (void *) inode_fid;
  806. }
  807. mutex_unlock(&v9inode->v_mutex);
  808. err = finish_open(file, dentry, generic_file_open);
  809. if (err)
  810. goto error;
  811. file->private_data = fid;
  812. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  813. v9fs_cache_inode_set_cookie(d_inode(dentry), file);
  814. file->f_mode |= FMODE_CREATED;
  815. out:
  816. dput(res);
  817. return err;
  818. error:
  819. if (fid)
  820. p9_client_clunk(fid);
  821. goto out;
  822. }
  823. /**
  824. * v9fs_vfs_unlink - VFS unlink hook to delete an inode
  825. * @i: inode that is being unlinked
  826. * @d: dentry that is being unlinked
  827. *
  828. */
  829. int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
  830. {
  831. return v9fs_remove(i, d, 0);
  832. }
  833. /**
  834. * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
  835. * @i: inode that is being unlinked
  836. * @d: dentry that is being unlinked
  837. *
  838. */
  839. int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
  840. {
  841. return v9fs_remove(i, d, AT_REMOVEDIR);
  842. }
  843. /**
  844. * v9fs_vfs_rename - VFS hook to rename an inode
  845. * @old_dir: old dir inode
  846. * @old_dentry: old dentry
  847. * @new_dir: new dir inode
  848. * @new_dentry: new dentry
  849. *
  850. */
  851. int
  852. v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  853. struct inode *new_dir, struct dentry *new_dentry,
  854. unsigned int flags)
  855. {
  856. int retval;
  857. struct inode *old_inode;
  858. struct inode *new_inode;
  859. struct v9fs_session_info *v9ses;
  860. struct p9_fid *oldfid;
  861. struct p9_fid *olddirfid;
  862. struct p9_fid *newdirfid;
  863. struct p9_wstat wstat;
  864. if (flags)
  865. return -EINVAL;
  866. p9_debug(P9_DEBUG_VFS, "\n");
  867. retval = 0;
  868. old_inode = d_inode(old_dentry);
  869. new_inode = d_inode(new_dentry);
  870. v9ses = v9fs_inode2v9ses(old_inode);
  871. oldfid = v9fs_fid_lookup(old_dentry);
  872. if (IS_ERR(oldfid))
  873. return PTR_ERR(oldfid);
  874. olddirfid = clone_fid(v9fs_parent_fid(old_dentry));
  875. if (IS_ERR(olddirfid)) {
  876. retval = PTR_ERR(olddirfid);
  877. goto done;
  878. }
  879. newdirfid = clone_fid(v9fs_parent_fid(new_dentry));
  880. if (IS_ERR(newdirfid)) {
  881. retval = PTR_ERR(newdirfid);
  882. goto clunk_olddir;
  883. }
  884. down_write(&v9ses->rename_sem);
  885. if (v9fs_proto_dotl(v9ses)) {
  886. retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
  887. newdirfid, new_dentry->d_name.name);
  888. if (retval == -EOPNOTSUPP)
  889. retval = p9_client_rename(oldfid, newdirfid,
  890. new_dentry->d_name.name);
  891. if (retval != -EOPNOTSUPP)
  892. goto clunk_newdir;
  893. }
  894. if (old_dentry->d_parent != new_dentry->d_parent) {
  895. /*
  896. * 9P .u can only handle file rename in the same directory
  897. */
  898. p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
  899. retval = -EXDEV;
  900. goto clunk_newdir;
  901. }
  902. v9fs_blank_wstat(&wstat);
  903. wstat.muid = v9ses->uname;
  904. wstat.name = new_dentry->d_name.name;
  905. retval = p9_client_wstat(oldfid, &wstat);
  906. clunk_newdir:
  907. if (!retval) {
  908. if (new_inode) {
  909. if (S_ISDIR(new_inode->i_mode))
  910. clear_nlink(new_inode);
  911. else
  912. v9fs_dec_count(new_inode);
  913. }
  914. if (S_ISDIR(old_inode->i_mode)) {
  915. if (!new_inode)
  916. inc_nlink(new_dir);
  917. v9fs_dec_count(old_dir);
  918. }
  919. v9fs_invalidate_inode_attr(old_inode);
  920. v9fs_invalidate_inode_attr(old_dir);
  921. v9fs_invalidate_inode_attr(new_dir);
  922. /* successful rename */
  923. d_move(old_dentry, new_dentry);
  924. }
  925. up_write(&v9ses->rename_sem);
  926. p9_client_clunk(newdirfid);
  927. clunk_olddir:
  928. p9_client_clunk(olddirfid);
  929. done:
  930. return retval;
  931. }
  932. /**
  933. * v9fs_vfs_getattr - retrieve file metadata
  934. * @path: Object to query
  935. * @stat: metadata structure to populate
  936. * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
  937. * @flags: AT_STATX_xxx setting
  938. *
  939. */
  940. static int
  941. v9fs_vfs_getattr(const struct path *path, struct kstat *stat,
  942. u32 request_mask, unsigned int flags)
  943. {
  944. struct dentry *dentry = path->dentry;
  945. struct v9fs_session_info *v9ses;
  946. struct p9_fid *fid;
  947. struct p9_wstat *st;
  948. p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
  949. v9ses = v9fs_dentry2v9ses(dentry);
  950. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  951. generic_fillattr(d_inode(dentry), stat);
  952. return 0;
  953. }
  954. fid = v9fs_fid_lookup(dentry);
  955. if (IS_ERR(fid))
  956. return PTR_ERR(fid);
  957. st = p9_client_stat(fid);
  958. if (IS_ERR(st))
  959. return PTR_ERR(st);
  960. v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
  961. generic_fillattr(d_inode(dentry), stat);
  962. p9stat_free(st);
  963. kfree(st);
  964. return 0;
  965. }
  966. /**
  967. * v9fs_vfs_setattr - set file metadata
  968. * @dentry: file whose metadata to set
  969. * @iattr: metadata assignment structure
  970. *
  971. */
  972. static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
  973. {
  974. int retval;
  975. struct v9fs_session_info *v9ses;
  976. struct p9_fid *fid;
  977. struct p9_wstat wstat;
  978. p9_debug(P9_DEBUG_VFS, "\n");
  979. retval = setattr_prepare(dentry, iattr);
  980. if (retval)
  981. return retval;
  982. retval = -EPERM;
  983. v9ses = v9fs_dentry2v9ses(dentry);
  984. fid = v9fs_fid_lookup(dentry);
  985. if(IS_ERR(fid))
  986. return PTR_ERR(fid);
  987. v9fs_blank_wstat(&wstat);
  988. if (iattr->ia_valid & ATTR_MODE)
  989. wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
  990. if (iattr->ia_valid & ATTR_MTIME)
  991. wstat.mtime = iattr->ia_mtime.tv_sec;
  992. if (iattr->ia_valid & ATTR_ATIME)
  993. wstat.atime = iattr->ia_atime.tv_sec;
  994. if (iattr->ia_valid & ATTR_SIZE)
  995. wstat.length = iattr->ia_size;
  996. if (v9fs_proto_dotu(v9ses)) {
  997. if (iattr->ia_valid & ATTR_UID)
  998. wstat.n_uid = iattr->ia_uid;
  999. if (iattr->ia_valid & ATTR_GID)
  1000. wstat.n_gid = iattr->ia_gid;
  1001. }
  1002. /* Write all dirty data */
  1003. if (d_is_reg(dentry))
  1004. filemap_write_and_wait(d_inode(dentry)->i_mapping);
  1005. retval = p9_client_wstat(fid, &wstat);
  1006. if (retval < 0)
  1007. return retval;
  1008. if ((iattr->ia_valid & ATTR_SIZE) &&
  1009. iattr->ia_size != i_size_read(d_inode(dentry)))
  1010. truncate_setsize(d_inode(dentry), iattr->ia_size);
  1011. v9fs_invalidate_inode_attr(d_inode(dentry));
  1012. setattr_copy(d_inode(dentry), iattr);
  1013. mark_inode_dirty(d_inode(dentry));
  1014. return 0;
  1015. }
  1016. /**
  1017. * v9fs_stat2inode - populate an inode structure with mistat info
  1018. * @stat: Plan 9 metadata (mistat) structure
  1019. * @inode: inode to populate
  1020. * @sb: superblock of filesystem
  1021. * @flags: control flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
  1022. *
  1023. */
  1024. void
  1025. v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
  1026. struct super_block *sb, unsigned int flags)
  1027. {
  1028. umode_t mode;
  1029. char ext[32];
  1030. char tag_name[14];
  1031. unsigned int i_nlink;
  1032. struct v9fs_session_info *v9ses = sb->s_fs_info;
  1033. struct v9fs_inode *v9inode = V9FS_I(inode);
  1034. set_nlink(inode, 1);
  1035. inode->i_atime.tv_sec = stat->atime;
  1036. inode->i_mtime.tv_sec = stat->mtime;
  1037. inode->i_ctime.tv_sec = stat->mtime;
  1038. inode->i_uid = v9ses->dfltuid;
  1039. inode->i_gid = v9ses->dfltgid;
  1040. if (v9fs_proto_dotu(v9ses)) {
  1041. inode->i_uid = stat->n_uid;
  1042. inode->i_gid = stat->n_gid;
  1043. }
  1044. if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
  1045. if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
  1046. /*
  1047. * Hadlink support got added later to
  1048. * to the .u extension. So there can be
  1049. * server out there that doesn't support
  1050. * this even with .u extension. So check
  1051. * for non NULL stat->extension
  1052. */
  1053. strlcpy(ext, stat->extension, sizeof(ext));
  1054. /* HARDLINKCOUNT %u */
  1055. sscanf(ext, "%13s %u", tag_name, &i_nlink);
  1056. if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
  1057. set_nlink(inode, i_nlink);
  1058. }
  1059. }
  1060. mode = p9mode2perm(v9ses, stat);
  1061. mode |= inode->i_mode & ~S_IALLUGO;
  1062. inode->i_mode = mode;
  1063. if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
  1064. v9fs_i_size_write(inode, stat->length);
  1065. /* not real number of blocks, but 512 byte ones ... */
  1066. inode->i_blocks = (stat->length + 512 - 1) >> 9;
  1067. v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
  1068. }
  1069. /**
  1070. * v9fs_qid2ino - convert qid into inode number
  1071. * @qid: qid to hash
  1072. *
  1073. * BUG: potential for inode number collisions?
  1074. */
  1075. ino_t v9fs_qid2ino(struct p9_qid *qid)
  1076. {
  1077. u64 path = qid->path + 2;
  1078. ino_t i = 0;
  1079. if (sizeof(ino_t) == sizeof(path))
  1080. memcpy(&i, &path, sizeof(ino_t));
  1081. else
  1082. i = (ino_t) (path ^ (path >> 32));
  1083. return i;
  1084. }
  1085. /**
  1086. * v9fs_vfs_get_link - follow a symlink path
  1087. * @dentry: dentry for symlink
  1088. * @inode: inode for symlink
  1089. * @done: delayed call for when we are done with the return value
  1090. */
  1091. static const char *v9fs_vfs_get_link(struct dentry *dentry,
  1092. struct inode *inode,
  1093. struct delayed_call *done)
  1094. {
  1095. struct v9fs_session_info *v9ses;
  1096. struct p9_fid *fid;
  1097. struct p9_wstat *st;
  1098. char *res;
  1099. if (!dentry)
  1100. return ERR_PTR(-ECHILD);
  1101. v9ses = v9fs_dentry2v9ses(dentry);
  1102. fid = v9fs_fid_lookup(dentry);
  1103. p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
  1104. if (IS_ERR(fid))
  1105. return ERR_CAST(fid);
  1106. if (!v9fs_proto_dotu(v9ses))
  1107. return ERR_PTR(-EBADF);
  1108. st = p9_client_stat(fid);
  1109. if (IS_ERR(st))
  1110. return ERR_CAST(st);
  1111. if (!(st->mode & P9_DMSYMLINK)) {
  1112. p9stat_free(st);
  1113. kfree(st);
  1114. return ERR_PTR(-EINVAL);
  1115. }
  1116. res = st->extension;
  1117. st->extension = NULL;
  1118. if (strlen(res) >= PATH_MAX)
  1119. res[PATH_MAX - 1] = '\0';
  1120. p9stat_free(st);
  1121. kfree(st);
  1122. set_delayed_call(done, kfree_link, res);
  1123. return res;
  1124. }
  1125. /**
  1126. * v9fs_vfs_mkspecial - create a special file
  1127. * @dir: inode to create special file in
  1128. * @dentry: dentry to create
  1129. * @perm: mode to create special file
  1130. * @extension: 9p2000.u format extension string representing special file
  1131. *
  1132. */
  1133. static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
  1134. u32 perm, const char *extension)
  1135. {
  1136. struct p9_fid *fid;
  1137. struct v9fs_session_info *v9ses;
  1138. v9ses = v9fs_inode2v9ses(dir);
  1139. if (!v9fs_proto_dotu(v9ses)) {
  1140. p9_debug(P9_DEBUG_ERROR, "not extended\n");
  1141. return -EPERM;
  1142. }
  1143. fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
  1144. P9_OREAD);
  1145. if (IS_ERR(fid))
  1146. return PTR_ERR(fid);
  1147. v9fs_invalidate_inode_attr(dir);
  1148. p9_client_clunk(fid);
  1149. return 0;
  1150. }
  1151. /**
  1152. * v9fs_vfs_symlink - helper function to create symlinks
  1153. * @dir: directory inode containing symlink
  1154. * @dentry: dentry for symlink
  1155. * @symname: symlink data
  1156. *
  1157. * See Also: 9P2000.u RFC for more information
  1158. *
  1159. */
  1160. static int
  1161. v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1162. {
  1163. p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n",
  1164. dir->i_ino, dentry, symname);
  1165. return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
  1166. }
  1167. #define U32_MAX_DIGITS 10
  1168. /**
  1169. * v9fs_vfs_link - create a hardlink
  1170. * @old_dentry: dentry for file to link to
  1171. * @dir: inode destination for new link
  1172. * @dentry: dentry for link
  1173. *
  1174. */
  1175. static int
  1176. v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
  1177. struct dentry *dentry)
  1178. {
  1179. int retval;
  1180. char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */
  1181. struct p9_fid *oldfid;
  1182. p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n",
  1183. dir->i_ino, dentry, old_dentry);
  1184. oldfid = v9fs_fid_clone(old_dentry);
  1185. if (IS_ERR(oldfid))
  1186. return PTR_ERR(oldfid);
  1187. sprintf(name, "%d\n", oldfid->fid);
  1188. retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
  1189. if (!retval) {
  1190. v9fs_refresh_inode(oldfid, d_inode(old_dentry));
  1191. v9fs_invalidate_inode_attr(dir);
  1192. }
  1193. p9_client_clunk(oldfid);
  1194. return retval;
  1195. }
  1196. /**
  1197. * v9fs_vfs_mknod - create a special file
  1198. * @dir: inode destination for new link
  1199. * @dentry: dentry for file
  1200. * @mode: mode for creation
  1201. * @rdev: device associated with special file
  1202. *
  1203. */
  1204. static int
  1205. v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  1206. {
  1207. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
  1208. int retval;
  1209. char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1];
  1210. u32 perm;
  1211. p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %hx MAJOR: %u MINOR: %u\n",
  1212. dir->i_ino, dentry, mode,
  1213. MAJOR(rdev), MINOR(rdev));
  1214. /* build extension */
  1215. if (S_ISBLK(mode))
  1216. sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
  1217. else if (S_ISCHR(mode))
  1218. sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
  1219. else
  1220. *name = 0;
  1221. perm = unixmode2p9mode(v9ses, mode);
  1222. retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
  1223. return retval;
  1224. }
  1225. int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
  1226. {
  1227. int umode;
  1228. dev_t rdev;
  1229. struct p9_wstat *st;
  1230. struct v9fs_session_info *v9ses;
  1231. unsigned int flags;
  1232. v9ses = v9fs_inode2v9ses(inode);
  1233. st = p9_client_stat(fid);
  1234. if (IS_ERR(st))
  1235. return PTR_ERR(st);
  1236. /*
  1237. * Don't update inode if the file type is different
  1238. */
  1239. umode = p9mode2unixmode(v9ses, st, &rdev);
  1240. if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
  1241. goto out;
  1242. /*
  1243. * We don't want to refresh inode->i_size,
  1244. * because we may have cached data
  1245. */
  1246. flags = (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) ?
  1247. V9FS_STAT2INODE_KEEP_ISIZE : 0;
  1248. v9fs_stat2inode(st, inode, inode->i_sb, flags);
  1249. out:
  1250. p9stat_free(st);
  1251. kfree(st);
  1252. return 0;
  1253. }
  1254. static const struct inode_operations v9fs_dir_inode_operations_dotu = {
  1255. .create = v9fs_vfs_create,
  1256. .lookup = v9fs_vfs_lookup,
  1257. .atomic_open = v9fs_vfs_atomic_open,
  1258. .symlink = v9fs_vfs_symlink,
  1259. .link = v9fs_vfs_link,
  1260. .unlink = v9fs_vfs_unlink,
  1261. .mkdir = v9fs_vfs_mkdir,
  1262. .rmdir = v9fs_vfs_rmdir,
  1263. .mknod = v9fs_vfs_mknod,
  1264. .rename = v9fs_vfs_rename,
  1265. .getattr = v9fs_vfs_getattr,
  1266. .setattr = v9fs_vfs_setattr,
  1267. };
  1268. static const struct inode_operations v9fs_dir_inode_operations = {
  1269. .create = v9fs_vfs_create,
  1270. .lookup = v9fs_vfs_lookup,
  1271. .atomic_open = v9fs_vfs_atomic_open,
  1272. .unlink = v9fs_vfs_unlink,
  1273. .mkdir = v9fs_vfs_mkdir,
  1274. .rmdir = v9fs_vfs_rmdir,
  1275. .mknod = v9fs_vfs_mknod,
  1276. .rename = v9fs_vfs_rename,
  1277. .getattr = v9fs_vfs_getattr,
  1278. .setattr = v9fs_vfs_setattr,
  1279. };
  1280. static const struct inode_operations v9fs_file_inode_operations = {
  1281. .getattr = v9fs_vfs_getattr,
  1282. .setattr = v9fs_vfs_setattr,
  1283. };
  1284. static const struct inode_operations v9fs_symlink_inode_operations = {
  1285. .get_link = v9fs_vfs_get_link,
  1286. .getattr = v9fs_vfs_getattr,
  1287. .setattr = v9fs_vfs_setattr,
  1288. };