vfs_inode.c 26 KB

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