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