vfs_inode.c 32 KB

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