vfs_inode.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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);
  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_remove - helper function to remove files and directories
  508. * @dir: directory inode that is being deleted
  509. * @dentry: dentry that is being deleted
  510. * @flags: removing a directory
  511. *
  512. */
  513. static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
  514. {
  515. struct inode *inode;
  516. int retval = -EOPNOTSUPP;
  517. struct p9_fid *v9fid, *dfid;
  518. struct v9fs_session_info *v9ses;
  519. p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
  520. dir, dentry, flags);
  521. v9ses = v9fs_inode2v9ses(dir);
  522. inode = d_inode(dentry);
  523. dfid = v9fs_parent_fid(dentry);
  524. if (IS_ERR(dfid)) {
  525. retval = PTR_ERR(dfid);
  526. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
  527. return retval;
  528. }
  529. if (v9fs_proto_dotl(v9ses))
  530. retval = p9_client_unlinkat(dfid, dentry->d_name.name,
  531. v9fs_at_to_dotl_flags(flags));
  532. if (retval == -EOPNOTSUPP) {
  533. /* Try the one based on path */
  534. v9fid = v9fs_fid_clone(dentry);
  535. if (IS_ERR(v9fid))
  536. return PTR_ERR(v9fid);
  537. retval = p9_client_remove(v9fid);
  538. }
  539. if (!retval) {
  540. /*
  541. * directories on unlink should have zero
  542. * link count
  543. */
  544. if (flags & AT_REMOVEDIR) {
  545. clear_nlink(inode);
  546. drop_nlink(dir);
  547. } else
  548. drop_nlink(inode);
  549. v9fs_invalidate_inode_attr(inode);
  550. v9fs_invalidate_inode_attr(dir);
  551. }
  552. return retval;
  553. }
  554. /**
  555. * v9fs_create - Create a file
  556. * @v9ses: session information
  557. * @dir: directory that dentry is being created in
  558. * @dentry: dentry that is being created
  559. * @extension: 9p2000.u extension string to support devices, etc.
  560. * @perm: create permissions
  561. * @mode: open mode
  562. *
  563. */
  564. static struct p9_fid *
  565. v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
  566. struct dentry *dentry, char *extension, u32 perm, u8 mode)
  567. {
  568. int err;
  569. char *name;
  570. struct p9_fid *dfid, *ofid, *fid;
  571. struct inode *inode;
  572. p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
  573. err = 0;
  574. ofid = NULL;
  575. fid = NULL;
  576. name = (char *) dentry->d_name.name;
  577. dfid = v9fs_parent_fid(dentry);
  578. if (IS_ERR(dfid)) {
  579. err = PTR_ERR(dfid);
  580. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  581. return ERR_PTR(err);
  582. }
  583. /* clone a fid to use for creation */
  584. ofid = clone_fid(dfid);
  585. if (IS_ERR(ofid)) {
  586. err = PTR_ERR(ofid);
  587. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  588. return ERR_PTR(err);
  589. }
  590. err = p9_client_fcreate(ofid, name, perm, mode, extension);
  591. if (err < 0) {
  592. p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
  593. goto error;
  594. }
  595. if (!(perm & P9_DMLINK)) {
  596. /* now walk from the parent so we can get unopened fid */
  597. fid = p9_client_walk(dfid, 1, &name, 1);
  598. if (IS_ERR(fid)) {
  599. err = PTR_ERR(fid);
  600. p9_debug(P9_DEBUG_VFS,
  601. "p9_client_walk failed %d\n", err);
  602. fid = NULL;
  603. goto error;
  604. }
  605. /*
  606. * instantiate inode and assign the unopened fid to the dentry
  607. */
  608. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  609. if (IS_ERR(inode)) {
  610. err = PTR_ERR(inode);
  611. p9_debug(P9_DEBUG_VFS,
  612. "inode creation failed %d\n", err);
  613. goto error;
  614. }
  615. v9fs_fid_add(dentry, fid);
  616. d_instantiate(dentry, inode);
  617. }
  618. return ofid;
  619. error:
  620. if (ofid)
  621. p9_client_clunk(ofid);
  622. if (fid)
  623. p9_client_clunk(fid);
  624. return ERR_PTR(err);
  625. }
  626. /**
  627. * v9fs_vfs_create - VFS hook to create a regular file
  628. *
  629. * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open(). This is only called
  630. * for mknod(2).
  631. *
  632. * @dir: directory inode that is being created
  633. * @dentry: dentry that is being deleted
  634. * @mode: create permissions
  635. *
  636. */
  637. static int
  638. v9fs_vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  639. bool excl)
  640. {
  641. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
  642. u32 perm = unixmode2p9mode(v9ses, mode);
  643. struct p9_fid *fid;
  644. /* P9_OEXCL? */
  645. fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
  646. if (IS_ERR(fid))
  647. return PTR_ERR(fid);
  648. v9fs_invalidate_inode_attr(dir);
  649. p9_client_clunk(fid);
  650. return 0;
  651. }
  652. /**
  653. * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
  654. * @dir: inode that is being unlinked
  655. * @dentry: dentry that is being unlinked
  656. * @mode: mode for new directory
  657. *
  658. */
  659. static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  660. {
  661. int err;
  662. u32 perm;
  663. struct p9_fid *fid;
  664. struct v9fs_session_info *v9ses;
  665. p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
  666. err = 0;
  667. v9ses = v9fs_inode2v9ses(dir);
  668. perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
  669. fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
  670. if (IS_ERR(fid)) {
  671. err = PTR_ERR(fid);
  672. fid = NULL;
  673. } else {
  674. inc_nlink(dir);
  675. v9fs_invalidate_inode_attr(dir);
  676. }
  677. if (fid)
  678. p9_client_clunk(fid);
  679. return err;
  680. }
  681. /**
  682. * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
  683. * @dir: inode that is being walked from
  684. * @dentry: dentry that is being walked to?
  685. * @flags: lookup flags (unused)
  686. *
  687. */
  688. struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
  689. unsigned int flags)
  690. {
  691. struct dentry *res;
  692. struct v9fs_session_info *v9ses;
  693. struct p9_fid *dfid, *fid;
  694. struct inode *inode;
  695. char *name;
  696. p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
  697. dir, dentry, dentry, flags);
  698. if (dentry->d_name.len > NAME_MAX)
  699. return ERR_PTR(-ENAMETOOLONG);
  700. v9ses = v9fs_inode2v9ses(dir);
  701. /* We can walk d_parent because we hold the dir->i_mutex */
  702. dfid = v9fs_parent_fid(dentry);
  703. if (IS_ERR(dfid))
  704. return ERR_CAST(dfid);
  705. name = (char *) dentry->d_name.name;
  706. fid = p9_client_walk(dfid, 1, &name, 1);
  707. if (IS_ERR(fid)) {
  708. if (fid == ERR_PTR(-ENOENT)) {
  709. d_add(dentry, NULL);
  710. return NULL;
  711. }
  712. return ERR_CAST(fid);
  713. }
  714. /*
  715. * Make sure we don't use a wrong inode due to parallel
  716. * unlink. For cached mode create calls request for new
  717. * inode. But with cache disabled, lookup should do this.
  718. */
  719. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  720. inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
  721. else
  722. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  723. if (IS_ERR(inode)) {
  724. p9_client_clunk(fid);
  725. return ERR_CAST(inode);
  726. }
  727. /*
  728. * If we had a rename on the server and a parallel lookup
  729. * for the new name, then make sure we instantiate with
  730. * the new name. ie look up for a/b, while on server somebody
  731. * moved b under k and client parallely did a lookup for
  732. * k/b.
  733. */
  734. res = d_splice_alias(inode, dentry);
  735. if (!res)
  736. v9fs_fid_add(dentry, fid);
  737. else if (!IS_ERR(res))
  738. v9fs_fid_add(res, fid);
  739. else
  740. p9_client_clunk(fid);
  741. return res;
  742. }
  743. static int
  744. v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
  745. struct file *file, unsigned flags, umode_t mode,
  746. int *opened)
  747. {
  748. int err;
  749. u32 perm;
  750. struct v9fs_inode *v9inode;
  751. struct v9fs_session_info *v9ses;
  752. struct p9_fid *fid, *inode_fid;
  753. struct dentry *res = NULL;
  754. if (d_in_lookup(dentry)) {
  755. res = v9fs_vfs_lookup(dir, dentry, 0);
  756. if (IS_ERR(res))
  757. return PTR_ERR(res);
  758. if (res)
  759. dentry = res;
  760. }
  761. /* Only creates */
  762. if (!(flags & O_CREAT) || d_really_is_positive(dentry))
  763. return finish_no_open(file, res);
  764. err = 0;
  765. v9ses = v9fs_inode2v9ses(dir);
  766. perm = unixmode2p9mode(v9ses, mode);
  767. fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
  768. v9fs_uflags2omode(flags,
  769. v9fs_proto_dotu(v9ses)));
  770. if (IS_ERR(fid)) {
  771. err = PTR_ERR(fid);
  772. fid = NULL;
  773. goto error;
  774. }
  775. v9fs_invalidate_inode_attr(dir);
  776. v9inode = V9FS_I(d_inode(dentry));
  777. mutex_lock(&v9inode->v_mutex);
  778. if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
  779. !v9inode->writeback_fid &&
  780. ((flags & O_ACCMODE) != O_RDONLY)) {
  781. /*
  782. * clone a fid and add it to writeback_fid
  783. * we do it during open time instead of
  784. * page dirty time via write_begin/page_mkwrite
  785. * because we want write after unlink usecase
  786. * to work.
  787. */
  788. inode_fid = v9fs_writeback_fid(dentry);
  789. if (IS_ERR(inode_fid)) {
  790. err = PTR_ERR(inode_fid);
  791. mutex_unlock(&v9inode->v_mutex);
  792. goto error;
  793. }
  794. v9inode->writeback_fid = (void *) inode_fid;
  795. }
  796. mutex_unlock(&v9inode->v_mutex);
  797. err = finish_open(file, dentry, generic_file_open, opened);
  798. if (err)
  799. goto error;
  800. file->private_data = fid;
  801. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  802. v9fs_cache_inode_set_cookie(d_inode(dentry), file);
  803. *opened |= FILE_CREATED;
  804. out:
  805. dput(res);
  806. return err;
  807. error:
  808. if (fid)
  809. p9_client_clunk(fid);
  810. goto out;
  811. }
  812. /**
  813. * v9fs_vfs_unlink - VFS unlink hook to delete an inode
  814. * @i: inode that is being unlinked
  815. * @d: dentry that is being unlinked
  816. *
  817. */
  818. int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
  819. {
  820. return v9fs_remove(i, d, 0);
  821. }
  822. /**
  823. * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
  824. * @i: inode that is being unlinked
  825. * @d: dentry that is being unlinked
  826. *
  827. */
  828. int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
  829. {
  830. return v9fs_remove(i, d, AT_REMOVEDIR);
  831. }
  832. /**
  833. * v9fs_vfs_rename - VFS hook to rename an inode
  834. * @old_dir: old dir inode
  835. * @old_dentry: old dentry
  836. * @new_dir: new dir inode
  837. * @new_dentry: new dentry
  838. *
  839. */
  840. int
  841. v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  842. struct inode *new_dir, struct dentry *new_dentry,
  843. unsigned int flags)
  844. {
  845. int retval;
  846. struct inode *old_inode;
  847. struct inode *new_inode;
  848. struct v9fs_session_info *v9ses;
  849. struct p9_fid *oldfid;
  850. struct p9_fid *olddirfid;
  851. struct p9_fid *newdirfid;
  852. struct p9_wstat wstat;
  853. if (flags)
  854. return -EINVAL;
  855. p9_debug(P9_DEBUG_VFS, "\n");
  856. retval = 0;
  857. old_inode = d_inode(old_dentry);
  858. new_inode = d_inode(new_dentry);
  859. v9ses = v9fs_inode2v9ses(old_inode);
  860. oldfid = v9fs_fid_lookup(old_dentry);
  861. if (IS_ERR(oldfid))
  862. return PTR_ERR(oldfid);
  863. olddirfid = clone_fid(v9fs_parent_fid(old_dentry));
  864. if (IS_ERR(olddirfid)) {
  865. retval = PTR_ERR(olddirfid);
  866. goto done;
  867. }
  868. newdirfid = clone_fid(v9fs_parent_fid(new_dentry));
  869. if (IS_ERR(newdirfid)) {
  870. retval = PTR_ERR(newdirfid);
  871. goto clunk_olddir;
  872. }
  873. down_write(&v9ses->rename_sem);
  874. if (v9fs_proto_dotl(v9ses)) {
  875. retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
  876. newdirfid, new_dentry->d_name.name);
  877. if (retval == -EOPNOTSUPP)
  878. retval = p9_client_rename(oldfid, newdirfid,
  879. new_dentry->d_name.name);
  880. if (retval != -EOPNOTSUPP)
  881. goto clunk_newdir;
  882. }
  883. if (old_dentry->d_parent != new_dentry->d_parent) {
  884. /*
  885. * 9P .u can only handle file rename in the same directory
  886. */
  887. p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
  888. retval = -EXDEV;
  889. goto clunk_newdir;
  890. }
  891. v9fs_blank_wstat(&wstat);
  892. wstat.muid = v9ses->uname;
  893. wstat.name = (char *) new_dentry->d_name.name;
  894. retval = p9_client_wstat(oldfid, &wstat);
  895. clunk_newdir:
  896. if (!retval) {
  897. if (new_inode) {
  898. if (S_ISDIR(new_inode->i_mode))
  899. clear_nlink(new_inode);
  900. else
  901. drop_nlink(new_inode);
  902. }
  903. if (S_ISDIR(old_inode->i_mode)) {
  904. if (!new_inode)
  905. inc_nlink(new_dir);
  906. drop_nlink(old_dir);
  907. }
  908. v9fs_invalidate_inode_attr(old_inode);
  909. v9fs_invalidate_inode_attr(old_dir);
  910. v9fs_invalidate_inode_attr(new_dir);
  911. /* successful rename */
  912. d_move(old_dentry, new_dentry);
  913. }
  914. up_write(&v9ses->rename_sem);
  915. p9_client_clunk(newdirfid);
  916. clunk_olddir:
  917. p9_client_clunk(olddirfid);
  918. done:
  919. return retval;
  920. }
  921. /**
  922. * v9fs_vfs_getattr - retrieve file metadata
  923. * @mnt: mount information
  924. * @dentry: file to get attributes on
  925. * @stat: metadata structure to populate
  926. *
  927. */
  928. static int
  929. v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  930. struct kstat *stat)
  931. {
  932. struct v9fs_session_info *v9ses;
  933. struct p9_fid *fid;
  934. struct p9_wstat *st;
  935. p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
  936. v9ses = v9fs_dentry2v9ses(dentry);
  937. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  938. generic_fillattr(d_inode(dentry), stat);
  939. return 0;
  940. }
  941. fid = v9fs_fid_lookup(dentry);
  942. if (IS_ERR(fid))
  943. return PTR_ERR(fid);
  944. st = p9_client_stat(fid);
  945. if (IS_ERR(st))
  946. return PTR_ERR(st);
  947. v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb);
  948. generic_fillattr(d_inode(dentry), stat);
  949. p9stat_free(st);
  950. kfree(st);
  951. return 0;
  952. }
  953. /**
  954. * v9fs_vfs_setattr - set file metadata
  955. * @dentry: file whose metadata to set
  956. * @iattr: metadata assignment structure
  957. *
  958. */
  959. static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
  960. {
  961. int retval;
  962. struct v9fs_session_info *v9ses;
  963. struct p9_fid *fid;
  964. struct p9_wstat wstat;
  965. p9_debug(P9_DEBUG_VFS, "\n");
  966. retval = setattr_prepare(dentry, iattr);
  967. if (retval)
  968. return retval;
  969. retval = -EPERM;
  970. v9ses = v9fs_dentry2v9ses(dentry);
  971. fid = v9fs_fid_lookup(dentry);
  972. if(IS_ERR(fid))
  973. return PTR_ERR(fid);
  974. v9fs_blank_wstat(&wstat);
  975. if (iattr->ia_valid & ATTR_MODE)
  976. wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
  977. if (iattr->ia_valid & ATTR_MTIME)
  978. wstat.mtime = iattr->ia_mtime.tv_sec;
  979. if (iattr->ia_valid & ATTR_ATIME)
  980. wstat.atime = iattr->ia_atime.tv_sec;
  981. if (iattr->ia_valid & ATTR_SIZE)
  982. wstat.length = iattr->ia_size;
  983. if (v9fs_proto_dotu(v9ses)) {
  984. if (iattr->ia_valid & ATTR_UID)
  985. wstat.n_uid = iattr->ia_uid;
  986. if (iattr->ia_valid & ATTR_GID)
  987. wstat.n_gid = iattr->ia_gid;
  988. }
  989. /* Write all dirty data */
  990. if (d_is_reg(dentry))
  991. filemap_write_and_wait(d_inode(dentry)->i_mapping);
  992. retval = p9_client_wstat(fid, &wstat);
  993. if (retval < 0)
  994. return retval;
  995. if ((iattr->ia_valid & ATTR_SIZE) &&
  996. iattr->ia_size != i_size_read(d_inode(dentry)))
  997. truncate_setsize(d_inode(dentry), iattr->ia_size);
  998. v9fs_invalidate_inode_attr(d_inode(dentry));
  999. setattr_copy(d_inode(dentry), iattr);
  1000. mark_inode_dirty(d_inode(dentry));
  1001. return 0;
  1002. }
  1003. /**
  1004. * v9fs_stat2inode - populate an inode structure with mistat info
  1005. * @stat: Plan 9 metadata (mistat) structure
  1006. * @inode: inode to populate
  1007. * @sb: superblock of filesystem
  1008. *
  1009. */
  1010. void
  1011. v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
  1012. struct super_block *sb)
  1013. {
  1014. umode_t mode;
  1015. char ext[32];
  1016. char tag_name[14];
  1017. unsigned int i_nlink;
  1018. struct v9fs_session_info *v9ses = sb->s_fs_info;
  1019. struct v9fs_inode *v9inode = V9FS_I(inode);
  1020. set_nlink(inode, 1);
  1021. inode->i_atime.tv_sec = stat->atime;
  1022. inode->i_mtime.tv_sec = stat->mtime;
  1023. inode->i_ctime.tv_sec = stat->mtime;
  1024. inode->i_uid = v9ses->dfltuid;
  1025. inode->i_gid = v9ses->dfltgid;
  1026. if (v9fs_proto_dotu(v9ses)) {
  1027. inode->i_uid = stat->n_uid;
  1028. inode->i_gid = stat->n_gid;
  1029. }
  1030. if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
  1031. if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
  1032. /*
  1033. * Hadlink support got added later to
  1034. * to the .u extension. So there can be
  1035. * server out there that doesn't support
  1036. * this even with .u extension. So check
  1037. * for non NULL stat->extension
  1038. */
  1039. strlcpy(ext, stat->extension, sizeof(ext));
  1040. /* HARDLINKCOUNT %u */
  1041. sscanf(ext, "%13s %u", tag_name, &i_nlink);
  1042. if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
  1043. set_nlink(inode, i_nlink);
  1044. }
  1045. }
  1046. mode = p9mode2perm(v9ses, stat);
  1047. mode |= inode->i_mode & ~S_IALLUGO;
  1048. inode->i_mode = mode;
  1049. i_size_write(inode, stat->length);
  1050. /* not real number of blocks, but 512 byte ones ... */
  1051. inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
  1052. v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
  1053. }
  1054. /**
  1055. * v9fs_qid2ino - convert qid into inode number
  1056. * @qid: qid to hash
  1057. *
  1058. * BUG: potential for inode number collisions?
  1059. */
  1060. ino_t v9fs_qid2ino(struct p9_qid *qid)
  1061. {
  1062. u64 path = qid->path + 2;
  1063. ino_t i = 0;
  1064. if (sizeof(ino_t) == sizeof(path))
  1065. memcpy(&i, &path, sizeof(ino_t));
  1066. else
  1067. i = (ino_t) (path ^ (path >> 32));
  1068. return i;
  1069. }
  1070. /**
  1071. * v9fs_vfs_get_link - follow a symlink path
  1072. * @dentry: dentry for symlink
  1073. * @inode: inode for symlink
  1074. * @done: delayed call for when we are done with the return value
  1075. */
  1076. static const char *v9fs_vfs_get_link(struct dentry *dentry,
  1077. struct inode *inode,
  1078. struct delayed_call *done)
  1079. {
  1080. struct v9fs_session_info *v9ses;
  1081. struct p9_fid *fid;
  1082. struct p9_wstat *st;
  1083. char *res;
  1084. if (!dentry)
  1085. return ERR_PTR(-ECHILD);
  1086. v9ses = v9fs_dentry2v9ses(dentry);
  1087. fid = v9fs_fid_lookup(dentry);
  1088. p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
  1089. if (IS_ERR(fid))
  1090. return ERR_CAST(fid);
  1091. if (!v9fs_proto_dotu(v9ses))
  1092. return ERR_PTR(-EBADF);
  1093. st = p9_client_stat(fid);
  1094. if (IS_ERR(st))
  1095. return ERR_CAST(st);
  1096. if (!(st->mode & P9_DMSYMLINK)) {
  1097. p9stat_free(st);
  1098. kfree(st);
  1099. return ERR_PTR(-EINVAL);
  1100. }
  1101. res = st->extension;
  1102. st->extension = NULL;
  1103. if (strlen(res) >= PATH_MAX)
  1104. res[PATH_MAX - 1] = '\0';
  1105. p9stat_free(st);
  1106. kfree(st);
  1107. set_delayed_call(done, kfree_link, res);
  1108. return res;
  1109. }
  1110. /**
  1111. * v9fs_vfs_mkspecial - create a special file
  1112. * @dir: inode to create special file in
  1113. * @dentry: dentry to create
  1114. * @perm: mode to create special file
  1115. * @extension: 9p2000.u format extension string representing special file
  1116. *
  1117. */
  1118. static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
  1119. u32 perm, const char *extension)
  1120. {
  1121. struct p9_fid *fid;
  1122. struct v9fs_session_info *v9ses;
  1123. v9ses = v9fs_inode2v9ses(dir);
  1124. if (!v9fs_proto_dotu(v9ses)) {
  1125. p9_debug(P9_DEBUG_ERROR, "not extended\n");
  1126. return -EPERM;
  1127. }
  1128. fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
  1129. P9_OREAD);
  1130. if (IS_ERR(fid))
  1131. return PTR_ERR(fid);
  1132. v9fs_invalidate_inode_attr(dir);
  1133. p9_client_clunk(fid);
  1134. return 0;
  1135. }
  1136. /**
  1137. * v9fs_vfs_symlink - helper function to create symlinks
  1138. * @dir: directory inode containing symlink
  1139. * @dentry: dentry for symlink
  1140. * @symname: symlink data
  1141. *
  1142. * See Also: 9P2000.u RFC for more information
  1143. *
  1144. */
  1145. static int
  1146. v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1147. {
  1148. p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n",
  1149. dir->i_ino, dentry, symname);
  1150. return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
  1151. }
  1152. #define U32_MAX_DIGITS 10
  1153. /**
  1154. * v9fs_vfs_link - create a hardlink
  1155. * @old_dentry: dentry for file to link to
  1156. * @dir: inode destination for new link
  1157. * @dentry: dentry for link
  1158. *
  1159. */
  1160. static int
  1161. v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
  1162. struct dentry *dentry)
  1163. {
  1164. int retval;
  1165. char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */
  1166. struct p9_fid *oldfid;
  1167. p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n",
  1168. dir->i_ino, dentry, old_dentry);
  1169. oldfid = v9fs_fid_clone(old_dentry);
  1170. if (IS_ERR(oldfid))
  1171. return PTR_ERR(oldfid);
  1172. sprintf(name, "%d\n", oldfid->fid);
  1173. retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
  1174. if (!retval) {
  1175. v9fs_refresh_inode(oldfid, d_inode(old_dentry));
  1176. v9fs_invalidate_inode_attr(dir);
  1177. }
  1178. p9_client_clunk(oldfid);
  1179. return retval;
  1180. }
  1181. /**
  1182. * v9fs_vfs_mknod - create a special file
  1183. * @dir: inode destination for new link
  1184. * @dentry: dentry for file
  1185. * @mode: mode for creation
  1186. * @rdev: device associated with special file
  1187. *
  1188. */
  1189. static int
  1190. v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  1191. {
  1192. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
  1193. int retval;
  1194. char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1];
  1195. u32 perm;
  1196. p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %hx MAJOR: %u MINOR: %u\n",
  1197. dir->i_ino, dentry, mode,
  1198. MAJOR(rdev), MINOR(rdev));
  1199. /* build extension */
  1200. if (S_ISBLK(mode))
  1201. sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
  1202. else if (S_ISCHR(mode))
  1203. sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
  1204. else
  1205. *name = 0;
  1206. perm = unixmode2p9mode(v9ses, mode);
  1207. retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
  1208. return retval;
  1209. }
  1210. int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
  1211. {
  1212. int umode;
  1213. dev_t rdev;
  1214. loff_t i_size;
  1215. struct p9_wstat *st;
  1216. struct v9fs_session_info *v9ses;
  1217. v9ses = v9fs_inode2v9ses(inode);
  1218. st = p9_client_stat(fid);
  1219. if (IS_ERR(st))
  1220. return PTR_ERR(st);
  1221. /*
  1222. * Don't update inode if the file type is different
  1223. */
  1224. umode = p9mode2unixmode(v9ses, st, &rdev);
  1225. if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
  1226. goto out;
  1227. spin_lock(&inode->i_lock);
  1228. /*
  1229. * We don't want to refresh inode->i_size,
  1230. * because we may have cached data
  1231. */
  1232. i_size = inode->i_size;
  1233. v9fs_stat2inode(st, inode, inode->i_sb);
  1234. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  1235. inode->i_size = i_size;
  1236. spin_unlock(&inode->i_lock);
  1237. out:
  1238. p9stat_free(st);
  1239. kfree(st);
  1240. return 0;
  1241. }
  1242. static const struct inode_operations v9fs_dir_inode_operations_dotu = {
  1243. .create = v9fs_vfs_create,
  1244. .lookup = v9fs_vfs_lookup,
  1245. .atomic_open = v9fs_vfs_atomic_open,
  1246. .symlink = v9fs_vfs_symlink,
  1247. .link = v9fs_vfs_link,
  1248. .unlink = v9fs_vfs_unlink,
  1249. .mkdir = v9fs_vfs_mkdir,
  1250. .rmdir = v9fs_vfs_rmdir,
  1251. .mknod = v9fs_vfs_mknod,
  1252. .rename = v9fs_vfs_rename,
  1253. .getattr = v9fs_vfs_getattr,
  1254. .setattr = v9fs_vfs_setattr,
  1255. };
  1256. static const struct inode_operations v9fs_dir_inode_operations = {
  1257. .create = v9fs_vfs_create,
  1258. .lookup = v9fs_vfs_lookup,
  1259. .atomic_open = v9fs_vfs_atomic_open,
  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_file_inode_operations = {
  1269. .getattr = v9fs_vfs_getattr,
  1270. .setattr = v9fs_vfs_setattr,
  1271. };
  1272. static const struct inode_operations v9fs_symlink_inode_operations = {
  1273. .readlink = generic_readlink,
  1274. .get_link = v9fs_vfs_get_link,
  1275. .getattr = v9fs_vfs_getattr,
  1276. .setattr = v9fs_vfs_setattr,
  1277. };