vfs_inode.c 29 KB

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