vfs_inode.c 28 KB

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