vfs_inode.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  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_fsgid_for_create - Helper function to get the gid for creating a
  208. * new file system object. This checks the S_ISGID to determine the owning
  209. * group of the new file system object.
  210. */
  211. static gid_t v9fs_get_fsgid_for_create(struct inode *dir_inode)
  212. {
  213. BUG_ON(dir_inode == NULL);
  214. if (dir_inode->i_mode & S_ISGID) {
  215. /* set_gid bit is set.*/
  216. return dir_inode->i_gid;
  217. }
  218. return current_fsgid();
  219. }
  220. /**
  221. * v9fs_dentry_from_dir_inode - helper function to get the dentry from
  222. * dir inode.
  223. *
  224. */
  225. struct dentry *v9fs_dentry_from_dir_inode(struct inode *inode)
  226. {
  227. struct dentry *dentry;
  228. spin_lock(&dcache_lock);
  229. /* Directory should have only one entry. */
  230. BUG_ON(S_ISDIR(inode->i_mode) && !list_is_singular(&inode->i_dentry));
  231. dentry = list_entry(inode->i_dentry.next, struct dentry, d_alias);
  232. spin_unlock(&dcache_lock);
  233. return dentry;
  234. }
  235. /**
  236. * v9fs_get_inode - helper function to setup an inode
  237. * @sb: superblock
  238. * @mode: mode to setup inode with
  239. *
  240. */
  241. struct inode *v9fs_get_inode(struct super_block *sb, int mode)
  242. {
  243. int err;
  244. struct inode *inode;
  245. struct v9fs_session_info *v9ses = sb->s_fs_info;
  246. P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
  247. inode = new_inode(sb);
  248. if (!inode) {
  249. P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
  250. return ERR_PTR(-ENOMEM);
  251. }
  252. inode_init_owner(inode, NULL, mode);
  253. inode->i_blocks = 0;
  254. inode->i_rdev = 0;
  255. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  256. inode->i_mapping->a_ops = &v9fs_addr_operations;
  257. switch (mode & S_IFMT) {
  258. case S_IFIFO:
  259. case S_IFBLK:
  260. case S_IFCHR:
  261. case S_IFSOCK:
  262. if (v9fs_proto_dotl(v9ses)) {
  263. inode->i_op = &v9fs_file_inode_operations_dotl;
  264. inode->i_fop = &v9fs_file_operations_dotl;
  265. } else if (v9fs_proto_dotu(v9ses)) {
  266. inode->i_op = &v9fs_file_inode_operations;
  267. inode->i_fop = &v9fs_file_operations;
  268. } else {
  269. P9_DPRINTK(P9_DEBUG_ERROR,
  270. "special files without extended mode\n");
  271. err = -EINVAL;
  272. goto error;
  273. }
  274. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  275. break;
  276. case S_IFREG:
  277. if (v9fs_proto_dotl(v9ses)) {
  278. inode->i_op = &v9fs_file_inode_operations_dotl;
  279. inode->i_fop = &v9fs_file_operations_dotl;
  280. } else {
  281. inode->i_op = &v9fs_file_inode_operations;
  282. inode->i_fop = &v9fs_file_operations;
  283. }
  284. break;
  285. case S_IFLNK:
  286. if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
  287. P9_DPRINTK(P9_DEBUG_ERROR, "extended modes used with "
  288. "legacy protocol.\n");
  289. err = -EINVAL;
  290. goto error;
  291. }
  292. if (v9fs_proto_dotl(v9ses))
  293. inode->i_op = &v9fs_symlink_inode_operations_dotl;
  294. else
  295. inode->i_op = &v9fs_symlink_inode_operations;
  296. break;
  297. case S_IFDIR:
  298. inc_nlink(inode);
  299. if (v9fs_proto_dotl(v9ses))
  300. inode->i_op = &v9fs_dir_inode_operations_dotl;
  301. else if (v9fs_proto_dotu(v9ses))
  302. inode->i_op = &v9fs_dir_inode_operations_dotu;
  303. else
  304. inode->i_op = &v9fs_dir_inode_operations;
  305. if (v9fs_proto_dotl(v9ses))
  306. inode->i_fop = &v9fs_dir_operations_dotl;
  307. else
  308. inode->i_fop = &v9fs_dir_operations;
  309. break;
  310. default:
  311. P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
  312. mode, mode & S_IFMT);
  313. err = -EINVAL;
  314. goto error;
  315. }
  316. return inode;
  317. error:
  318. iput(inode);
  319. return ERR_PTR(err);
  320. }
  321. /*
  322. static struct v9fs_fid*
  323. v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
  324. {
  325. int err;
  326. int nfid;
  327. struct v9fs_fid *ret;
  328. struct v9fs_fcall *fcall;
  329. nfid = v9fs_get_idpool(&v9ses->fidpool);
  330. if (nfid < 0) {
  331. eprintk(KERN_WARNING, "no free fids available\n");
  332. return ERR_PTR(-ENOSPC);
  333. }
  334. err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
  335. &fcall);
  336. if (err < 0) {
  337. if (fcall && fcall->id == RWALK)
  338. goto clunk_fid;
  339. PRINT_FCALL_ERROR("walk error", fcall);
  340. v9fs_put_idpool(nfid, &v9ses->fidpool);
  341. goto error;
  342. }
  343. kfree(fcall);
  344. fcall = NULL;
  345. ret = v9fs_fid_create(v9ses, nfid);
  346. if (!ret) {
  347. err = -ENOMEM;
  348. goto clunk_fid;
  349. }
  350. err = v9fs_fid_insert(ret, dentry);
  351. if (err < 0) {
  352. v9fs_fid_destroy(ret);
  353. goto clunk_fid;
  354. }
  355. return ret;
  356. clunk_fid:
  357. v9fs_t_clunk(v9ses, nfid);
  358. error:
  359. kfree(fcall);
  360. return ERR_PTR(err);
  361. }
  362. */
  363. /**
  364. * v9fs_clear_inode - release an inode
  365. * @inode: inode to release
  366. *
  367. */
  368. void v9fs_clear_inode(struct inode *inode)
  369. {
  370. filemap_fdatawrite(inode->i_mapping);
  371. #ifdef CONFIG_9P_FSCACHE
  372. v9fs_cache_inode_put_cookie(inode);
  373. #endif
  374. }
  375. static struct inode *
  376. v9fs_inode(struct v9fs_session_info *v9ses, struct p9_fid *fid,
  377. struct super_block *sb)
  378. {
  379. int err, umode;
  380. struct inode *ret = NULL;
  381. struct p9_wstat *st;
  382. st = p9_client_stat(fid);
  383. if (IS_ERR(st))
  384. return ERR_CAST(st);
  385. umode = p9mode2unixmode(v9ses, st->mode);
  386. ret = v9fs_get_inode(sb, umode);
  387. if (IS_ERR(ret)) {
  388. err = PTR_ERR(ret);
  389. goto error;
  390. }
  391. v9fs_stat2inode(st, ret, sb);
  392. ret->i_ino = v9fs_qid2ino(&st->qid);
  393. #ifdef CONFIG_9P_FSCACHE
  394. v9fs_vcookie_set_qid(ret, &st->qid);
  395. v9fs_cache_inode_get_cookie(ret);
  396. #endif
  397. p9stat_free(st);
  398. kfree(st);
  399. return ret;
  400. error:
  401. p9stat_free(st);
  402. kfree(st);
  403. return ERR_PTR(err);
  404. }
  405. static struct inode *
  406. v9fs_inode_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
  407. struct super_block *sb)
  408. {
  409. struct inode *ret = NULL;
  410. int err;
  411. struct p9_stat_dotl *st;
  412. st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
  413. if (IS_ERR(st))
  414. return ERR_CAST(st);
  415. ret = v9fs_get_inode(sb, st->st_mode);
  416. if (IS_ERR(ret)) {
  417. err = PTR_ERR(ret);
  418. goto error;
  419. }
  420. v9fs_stat2inode_dotl(st, ret);
  421. ret->i_ino = v9fs_qid2ino(&st->qid);
  422. #ifdef CONFIG_9P_FSCACHE
  423. v9fs_vcookie_set_qid(ret, &st->qid);
  424. v9fs_cache_inode_get_cookie(ret);
  425. #endif
  426. kfree(st);
  427. return ret;
  428. error:
  429. kfree(st);
  430. return ERR_PTR(err);
  431. }
  432. /**
  433. * v9fs_inode_from_fid - Helper routine to populate an inode by
  434. * issuing a attribute request
  435. * @v9ses: session information
  436. * @fid: fid to issue attribute request for
  437. * @sb: superblock on which to create inode
  438. *
  439. */
  440. static inline struct inode *
  441. v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
  442. struct super_block *sb)
  443. {
  444. if (v9fs_proto_dotl(v9ses))
  445. return v9fs_inode_dotl(v9ses, fid, sb);
  446. else
  447. return v9fs_inode(v9ses, fid, sb);
  448. }
  449. /**
  450. * v9fs_remove - helper function to remove files and directories
  451. * @dir: directory inode that is being deleted
  452. * @file: dentry that is being deleted
  453. * @rmdir: removing a directory
  454. *
  455. */
  456. static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
  457. {
  458. int retval;
  459. struct inode *file_inode;
  460. struct p9_fid *v9fid;
  461. P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
  462. rmdir);
  463. file_inode = file->d_inode;
  464. v9fid = v9fs_fid_clone(file);
  465. if (IS_ERR(v9fid))
  466. return PTR_ERR(v9fid);
  467. retval = p9_client_remove(v9fid);
  468. if (!retval)
  469. drop_nlink(file_inode);
  470. return retval;
  471. }
  472. static int
  473. v9fs_open_created(struct inode *inode, struct file *file)
  474. {
  475. return 0;
  476. }
  477. /**
  478. * v9fs_create - Create a file
  479. * @v9ses: session information
  480. * @dir: directory that dentry is being created in
  481. * @dentry: dentry that is being created
  482. * @extension: 9p2000.u extension string to support devices, etc.
  483. * @perm: create permissions
  484. * @mode: open mode
  485. *
  486. */
  487. static struct p9_fid *
  488. v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
  489. struct dentry *dentry, char *extension, u32 perm, u8 mode)
  490. {
  491. int err;
  492. char *name;
  493. struct p9_fid *dfid, *ofid, *fid;
  494. struct inode *inode;
  495. P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
  496. err = 0;
  497. ofid = NULL;
  498. fid = NULL;
  499. name = (char *) dentry->d_name.name;
  500. dfid = v9fs_fid_lookup(dentry->d_parent);
  501. if (IS_ERR(dfid)) {
  502. err = PTR_ERR(dfid);
  503. P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  504. return ERR_PTR(err);
  505. }
  506. /* clone a fid to use for creation */
  507. ofid = p9_client_walk(dfid, 0, NULL, 1);
  508. if (IS_ERR(ofid)) {
  509. err = PTR_ERR(ofid);
  510. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  511. return ERR_PTR(err);
  512. }
  513. err = p9_client_fcreate(ofid, name, perm, mode, extension);
  514. if (err < 0) {
  515. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
  516. goto error;
  517. }
  518. /* now walk from the parent so we can get unopened fid */
  519. fid = p9_client_walk(dfid, 1, &name, 1);
  520. if (IS_ERR(fid)) {
  521. err = PTR_ERR(fid);
  522. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  523. fid = NULL;
  524. goto error;
  525. }
  526. /* instantiate inode and assign the unopened fid to the dentry */
  527. inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
  528. if (IS_ERR(inode)) {
  529. err = PTR_ERR(inode);
  530. P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
  531. goto error;
  532. }
  533. if (v9ses->cache)
  534. dentry->d_op = &v9fs_cached_dentry_operations;
  535. else
  536. dentry->d_op = &v9fs_dentry_operations;
  537. d_instantiate(dentry, inode);
  538. err = v9fs_fid_add(dentry, fid);
  539. if (err < 0)
  540. goto error;
  541. return ofid;
  542. error:
  543. if (ofid)
  544. p9_client_clunk(ofid);
  545. if (fid)
  546. p9_client_clunk(fid);
  547. return ERR_PTR(err);
  548. }
  549. /**
  550. * v9fs_vfs_create - VFS hook to create files
  551. * @dir: directory inode that is being created
  552. * @dentry: dentry that is being deleted
  553. * @mode: create permissions
  554. * @nd: path information
  555. *
  556. */
  557. static int
  558. v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  559. struct nameidata *nd)
  560. {
  561. int err;
  562. u32 perm;
  563. int flags;
  564. struct v9fs_session_info *v9ses;
  565. struct p9_fid *fid;
  566. struct file *filp;
  567. err = 0;
  568. fid = NULL;
  569. v9ses = v9fs_inode2v9ses(dir);
  570. perm = unixmode2p9mode(v9ses, mode);
  571. if (nd && nd->flags & LOOKUP_OPEN)
  572. flags = nd->intent.open.flags - 1;
  573. else
  574. flags = O_RDWR;
  575. fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
  576. v9fs_uflags2omode(flags,
  577. v9fs_proto_dotu(v9ses)));
  578. if (IS_ERR(fid)) {
  579. err = PTR_ERR(fid);
  580. fid = NULL;
  581. goto error;
  582. }
  583. /* if we are opening a file, assign the open fid to the file */
  584. if (nd && nd->flags & LOOKUP_OPEN) {
  585. filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
  586. if (IS_ERR(filp)) {
  587. err = PTR_ERR(filp);
  588. goto error;
  589. }
  590. filp->private_data = fid;
  591. } else
  592. p9_client_clunk(fid);
  593. return 0;
  594. error:
  595. if (fid)
  596. p9_client_clunk(fid);
  597. return err;
  598. }
  599. /**
  600. * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
  601. * @dir: inode that is being unlinked
  602. * @dentry: dentry that is being unlinked
  603. * @mode: mode for new directory
  604. *
  605. */
  606. static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  607. {
  608. int err;
  609. u32 perm;
  610. struct v9fs_session_info *v9ses;
  611. struct p9_fid *fid;
  612. P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
  613. err = 0;
  614. v9ses = v9fs_inode2v9ses(dir);
  615. perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
  616. fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
  617. if (IS_ERR(fid)) {
  618. err = PTR_ERR(fid);
  619. fid = NULL;
  620. }
  621. if (fid)
  622. p9_client_clunk(fid);
  623. return err;
  624. }
  625. /**
  626. * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
  627. * @dir: inode that is being unlinked
  628. * @dentry: dentry that is being unlinked
  629. * @mode: mode for new directory
  630. *
  631. */
  632. static int v9fs_vfs_mkdir_dotl(struct inode *dir, struct dentry *dentry,
  633. int mode)
  634. {
  635. int err;
  636. struct v9fs_session_info *v9ses;
  637. struct p9_fid *fid = NULL, *dfid = NULL;
  638. gid_t gid;
  639. char *name;
  640. struct inode *inode;
  641. struct p9_qid qid;
  642. struct dentry *dir_dentry;
  643. P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
  644. err = 0;
  645. v9ses = v9fs_inode2v9ses(dir);
  646. mode |= S_IFDIR;
  647. dir_dentry = v9fs_dentry_from_dir_inode(dir);
  648. dfid = v9fs_fid_lookup(dir_dentry);
  649. if (IS_ERR(dfid)) {
  650. err = PTR_ERR(dfid);
  651. P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  652. dfid = NULL;
  653. goto error;
  654. }
  655. gid = v9fs_get_fsgid_for_create(dir);
  656. if (gid < 0) {
  657. P9_DPRINTK(P9_DEBUG_VFS, "v9fs_get_fsgid_for_create failed\n");
  658. goto error;
  659. }
  660. name = (char *) dentry->d_name.name;
  661. err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
  662. if (err < 0)
  663. goto error;
  664. /* instantiate inode and assign the unopened fid to the dentry */
  665. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  666. fid = p9_client_walk(dfid, 1, &name, 1);
  667. if (IS_ERR(fid)) {
  668. err = PTR_ERR(fid);
  669. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
  670. err);
  671. fid = NULL;
  672. goto error;
  673. }
  674. inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
  675. if (IS_ERR(inode)) {
  676. err = PTR_ERR(inode);
  677. P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
  678. err);
  679. goto error;
  680. }
  681. dentry->d_op = &v9fs_cached_dentry_operations;
  682. d_instantiate(dentry, inode);
  683. err = v9fs_fid_add(dentry, fid);
  684. if (err < 0)
  685. goto error;
  686. fid = NULL;
  687. }
  688. error:
  689. if (fid)
  690. p9_client_clunk(fid);
  691. return err;
  692. }
  693. /**
  694. * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
  695. * @dir: inode that is being walked from
  696. * @dentry: dentry that is being walked to?
  697. * @nameidata: path data
  698. *
  699. */
  700. static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
  701. struct nameidata *nameidata)
  702. {
  703. struct super_block *sb;
  704. struct v9fs_session_info *v9ses;
  705. struct p9_fid *dfid, *fid;
  706. struct inode *inode;
  707. char *name;
  708. int result = 0;
  709. P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
  710. dir, dentry->d_name.name, dentry, nameidata);
  711. if (dentry->d_name.len > NAME_MAX)
  712. return ERR_PTR(-ENAMETOOLONG);
  713. sb = dir->i_sb;
  714. v9ses = v9fs_inode2v9ses(dir);
  715. dfid = v9fs_fid_lookup(dentry->d_parent);
  716. if (IS_ERR(dfid))
  717. return ERR_CAST(dfid);
  718. name = (char *) dentry->d_name.name;
  719. fid = p9_client_walk(dfid, 1, &name, 1);
  720. if (IS_ERR(fid)) {
  721. result = PTR_ERR(fid);
  722. if (result == -ENOENT) {
  723. inode = NULL;
  724. goto inst_out;
  725. }
  726. return ERR_PTR(result);
  727. }
  728. inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
  729. if (IS_ERR(inode)) {
  730. result = PTR_ERR(inode);
  731. inode = NULL;
  732. goto error;
  733. }
  734. result = v9fs_fid_add(dentry, fid);
  735. if (result < 0)
  736. goto error;
  737. inst_out:
  738. if (v9ses->cache)
  739. dentry->d_op = &v9fs_cached_dentry_operations;
  740. else
  741. dentry->d_op = &v9fs_dentry_operations;
  742. d_add(dentry, inode);
  743. return NULL;
  744. error:
  745. p9_client_clunk(fid);
  746. return ERR_PTR(result);
  747. }
  748. /**
  749. * v9fs_vfs_unlink - VFS unlink hook to delete an inode
  750. * @i: inode that is being unlinked
  751. * @d: dentry that is being unlinked
  752. *
  753. */
  754. static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
  755. {
  756. return v9fs_remove(i, d, 0);
  757. }
  758. /**
  759. * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
  760. * @i: inode that is being unlinked
  761. * @d: dentry that is being unlinked
  762. *
  763. */
  764. static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
  765. {
  766. return v9fs_remove(i, d, 1);
  767. }
  768. /**
  769. * v9fs_vfs_rename - VFS hook to rename an inode
  770. * @old_dir: old dir inode
  771. * @old_dentry: old dentry
  772. * @new_dir: new dir inode
  773. * @new_dentry: new dentry
  774. *
  775. */
  776. static int
  777. v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  778. struct inode *new_dir, struct dentry *new_dentry)
  779. {
  780. struct inode *old_inode;
  781. struct v9fs_session_info *v9ses;
  782. struct p9_fid *oldfid;
  783. struct p9_fid *olddirfid;
  784. struct p9_fid *newdirfid;
  785. struct p9_wstat wstat;
  786. int retval;
  787. P9_DPRINTK(P9_DEBUG_VFS, "\n");
  788. retval = 0;
  789. old_inode = old_dentry->d_inode;
  790. v9ses = v9fs_inode2v9ses(old_inode);
  791. oldfid = v9fs_fid_lookup(old_dentry);
  792. if (IS_ERR(oldfid))
  793. return PTR_ERR(oldfid);
  794. olddirfid = v9fs_fid_clone(old_dentry->d_parent);
  795. if (IS_ERR(olddirfid)) {
  796. retval = PTR_ERR(olddirfid);
  797. goto done;
  798. }
  799. newdirfid = v9fs_fid_clone(new_dentry->d_parent);
  800. if (IS_ERR(newdirfid)) {
  801. retval = PTR_ERR(newdirfid);
  802. goto clunk_olddir;
  803. }
  804. if (v9fs_proto_dotl(v9ses)) {
  805. retval = p9_client_rename(oldfid, newdirfid,
  806. (char *) new_dentry->d_name.name);
  807. if (retval != -ENOSYS)
  808. goto clunk_newdir;
  809. }
  810. /* 9P can only handle file rename in the same directory */
  811. if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
  812. P9_DPRINTK(P9_DEBUG_ERROR,
  813. "old dir and new dir are different\n");
  814. retval = -EXDEV;
  815. goto clunk_newdir;
  816. }
  817. v9fs_blank_wstat(&wstat);
  818. wstat.muid = v9ses->uname;
  819. wstat.name = (char *) new_dentry->d_name.name;
  820. retval = p9_client_wstat(oldfid, &wstat);
  821. clunk_newdir:
  822. p9_client_clunk(newdirfid);
  823. clunk_olddir:
  824. p9_client_clunk(olddirfid);
  825. done:
  826. return retval;
  827. }
  828. /**
  829. * v9fs_vfs_getattr - retrieve file metadata
  830. * @mnt: mount information
  831. * @dentry: file to get attributes on
  832. * @stat: metadata structure to populate
  833. *
  834. */
  835. static int
  836. v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  837. struct kstat *stat)
  838. {
  839. int err;
  840. struct v9fs_session_info *v9ses;
  841. struct p9_fid *fid;
  842. struct p9_wstat *st;
  843. P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
  844. err = -EPERM;
  845. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  846. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  847. return simple_getattr(mnt, dentry, stat);
  848. fid = v9fs_fid_lookup(dentry);
  849. if (IS_ERR(fid))
  850. return PTR_ERR(fid);
  851. st = p9_client_stat(fid);
  852. if (IS_ERR(st))
  853. return PTR_ERR(st);
  854. v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
  855. generic_fillattr(dentry->d_inode, stat);
  856. kfree(st);
  857. return 0;
  858. }
  859. static int
  860. v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
  861. struct kstat *stat)
  862. {
  863. int err;
  864. struct v9fs_session_info *v9ses;
  865. struct p9_fid *fid;
  866. struct p9_stat_dotl *st;
  867. P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
  868. err = -EPERM;
  869. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  870. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  871. return simple_getattr(mnt, dentry, stat);
  872. fid = v9fs_fid_lookup(dentry);
  873. if (IS_ERR(fid))
  874. return PTR_ERR(fid);
  875. /* Ask for all the fields in stat structure. Server will return
  876. * whatever it supports
  877. */
  878. st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
  879. if (IS_ERR(st))
  880. return PTR_ERR(st);
  881. v9fs_stat2inode_dotl(st, dentry->d_inode);
  882. generic_fillattr(dentry->d_inode, stat);
  883. /* Change block size to what the server returned */
  884. stat->blksize = st->st_blksize;
  885. kfree(st);
  886. return 0;
  887. }
  888. /**
  889. * v9fs_vfs_setattr - set file metadata
  890. * @dentry: file whose metadata to set
  891. * @iattr: metadata assignment structure
  892. *
  893. */
  894. static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
  895. {
  896. int retval;
  897. struct v9fs_session_info *v9ses;
  898. struct p9_fid *fid;
  899. struct p9_wstat wstat;
  900. P9_DPRINTK(P9_DEBUG_VFS, "\n");
  901. retval = -EPERM;
  902. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  903. fid = v9fs_fid_lookup(dentry);
  904. if(IS_ERR(fid))
  905. return PTR_ERR(fid);
  906. v9fs_blank_wstat(&wstat);
  907. if (iattr->ia_valid & ATTR_MODE)
  908. wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
  909. if (iattr->ia_valid & ATTR_MTIME)
  910. wstat.mtime = iattr->ia_mtime.tv_sec;
  911. if (iattr->ia_valid & ATTR_ATIME)
  912. wstat.atime = iattr->ia_atime.tv_sec;
  913. if (iattr->ia_valid & ATTR_SIZE)
  914. wstat.length = iattr->ia_size;
  915. if (v9fs_proto_dotu(v9ses)) {
  916. if (iattr->ia_valid & ATTR_UID)
  917. wstat.n_uid = iattr->ia_uid;
  918. if (iattr->ia_valid & ATTR_GID)
  919. wstat.n_gid = iattr->ia_gid;
  920. }
  921. retval = p9_client_wstat(fid, &wstat);
  922. if (retval >= 0)
  923. retval = inode_setattr(dentry->d_inode, iattr);
  924. return retval;
  925. }
  926. /**
  927. * v9fs_vfs_setattr_dotl - set file metadata
  928. * @dentry: file whose metadata to set
  929. * @iattr: metadata assignment structure
  930. *
  931. */
  932. static int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
  933. {
  934. int retval;
  935. struct v9fs_session_info *v9ses;
  936. struct p9_fid *fid;
  937. struct p9_iattr_dotl p9attr;
  938. P9_DPRINTK(P9_DEBUG_VFS, "\n");
  939. retval = inode_change_ok(dentry->d_inode, iattr);
  940. if (retval)
  941. return retval;
  942. p9attr.valid = iattr->ia_valid;
  943. p9attr.mode = iattr->ia_mode;
  944. p9attr.uid = iattr->ia_uid;
  945. p9attr.gid = iattr->ia_gid;
  946. p9attr.size = iattr->ia_size;
  947. p9attr.atime_sec = iattr->ia_atime.tv_sec;
  948. p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
  949. p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
  950. p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
  951. retval = -EPERM;
  952. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  953. fid = v9fs_fid_lookup(dentry);
  954. if (IS_ERR(fid))
  955. return PTR_ERR(fid);
  956. retval = p9_client_setattr(fid, &p9attr);
  957. if (retval >= 0)
  958. retval = inode_setattr(dentry->d_inode, iattr);
  959. return retval;
  960. }
  961. /**
  962. * v9fs_stat2inode - populate an inode structure with mistat info
  963. * @stat: Plan 9 metadata (mistat) structure
  964. * @inode: inode to populate
  965. * @sb: superblock of filesystem
  966. *
  967. */
  968. void
  969. v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
  970. struct super_block *sb)
  971. {
  972. char ext[32];
  973. char tag_name[14];
  974. unsigned int i_nlink;
  975. struct v9fs_session_info *v9ses = sb->s_fs_info;
  976. inode->i_nlink = 1;
  977. inode->i_atime.tv_sec = stat->atime;
  978. inode->i_mtime.tv_sec = stat->mtime;
  979. inode->i_ctime.tv_sec = stat->mtime;
  980. inode->i_uid = v9ses->dfltuid;
  981. inode->i_gid = v9ses->dfltgid;
  982. if (v9fs_proto_dotu(v9ses)) {
  983. inode->i_uid = stat->n_uid;
  984. inode->i_gid = stat->n_gid;
  985. }
  986. if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
  987. if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
  988. /*
  989. * Hadlink support got added later to
  990. * to the .u extension. So there can be
  991. * server out there that doesn't support
  992. * this even with .u extension. So check
  993. * for non NULL stat->extension
  994. */
  995. strncpy(ext, stat->extension, sizeof(ext));
  996. /* HARDLINKCOUNT %u */
  997. sscanf(ext, "%13s %u", tag_name, &i_nlink);
  998. if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
  999. inode->i_nlink = i_nlink;
  1000. }
  1001. }
  1002. inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
  1003. if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
  1004. char type = 0;
  1005. int major = -1;
  1006. int minor = -1;
  1007. strncpy(ext, stat->extension, sizeof(ext));
  1008. sscanf(ext, "%c %u %u", &type, &major, &minor);
  1009. switch (type) {
  1010. case 'c':
  1011. inode->i_mode &= ~S_IFBLK;
  1012. inode->i_mode |= S_IFCHR;
  1013. break;
  1014. case 'b':
  1015. break;
  1016. default:
  1017. P9_DPRINTK(P9_DEBUG_ERROR,
  1018. "Unknown special type %c %s\n", type,
  1019. stat->extension);
  1020. };
  1021. inode->i_rdev = MKDEV(major, minor);
  1022. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1023. } else
  1024. inode->i_rdev = 0;
  1025. i_size_write(inode, stat->length);
  1026. /* not real number of blocks, but 512 byte ones ... */
  1027. inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
  1028. }
  1029. /**
  1030. * v9fs_stat2inode_dotl - populate an inode structure with stat info
  1031. * @stat: stat structure
  1032. * @inode: inode to populate
  1033. * @sb: superblock of filesystem
  1034. *
  1035. */
  1036. void
  1037. v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
  1038. {
  1039. if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
  1040. inode->i_atime.tv_sec = stat->st_atime_sec;
  1041. inode->i_atime.tv_nsec = stat->st_atime_nsec;
  1042. inode->i_mtime.tv_sec = stat->st_mtime_sec;
  1043. inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
  1044. inode->i_ctime.tv_sec = stat->st_ctime_sec;
  1045. inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
  1046. inode->i_uid = stat->st_uid;
  1047. inode->i_gid = stat->st_gid;
  1048. inode->i_nlink = stat->st_nlink;
  1049. inode->i_mode = stat->st_mode;
  1050. inode->i_rdev = new_decode_dev(stat->st_rdev);
  1051. if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode)))
  1052. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1053. i_size_write(inode, stat->st_size);
  1054. inode->i_blocks = stat->st_blocks;
  1055. } else {
  1056. if (stat->st_result_mask & P9_STATS_ATIME) {
  1057. inode->i_atime.tv_sec = stat->st_atime_sec;
  1058. inode->i_atime.tv_nsec = stat->st_atime_nsec;
  1059. }
  1060. if (stat->st_result_mask & P9_STATS_MTIME) {
  1061. inode->i_mtime.tv_sec = stat->st_mtime_sec;
  1062. inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
  1063. }
  1064. if (stat->st_result_mask & P9_STATS_CTIME) {
  1065. inode->i_ctime.tv_sec = stat->st_ctime_sec;
  1066. inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
  1067. }
  1068. if (stat->st_result_mask & P9_STATS_UID)
  1069. inode->i_uid = stat->st_uid;
  1070. if (stat->st_result_mask & P9_STATS_GID)
  1071. inode->i_gid = stat->st_gid;
  1072. if (stat->st_result_mask & P9_STATS_NLINK)
  1073. inode->i_nlink = stat->st_nlink;
  1074. if (stat->st_result_mask & P9_STATS_MODE) {
  1075. inode->i_mode = stat->st_mode;
  1076. if ((S_ISBLK(inode->i_mode)) ||
  1077. (S_ISCHR(inode->i_mode)))
  1078. init_special_inode(inode, inode->i_mode,
  1079. inode->i_rdev);
  1080. }
  1081. if (stat->st_result_mask & P9_STATS_RDEV)
  1082. inode->i_rdev = new_decode_dev(stat->st_rdev);
  1083. if (stat->st_result_mask & P9_STATS_SIZE)
  1084. i_size_write(inode, stat->st_size);
  1085. if (stat->st_result_mask & P9_STATS_BLOCKS)
  1086. inode->i_blocks = stat->st_blocks;
  1087. }
  1088. if (stat->st_result_mask & P9_STATS_GEN)
  1089. inode->i_generation = stat->st_gen;
  1090. /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
  1091. * because the inode structure does not have fields for them.
  1092. */
  1093. }
  1094. /**
  1095. * v9fs_qid2ino - convert qid into inode number
  1096. * @qid: qid to hash
  1097. *
  1098. * BUG: potential for inode number collisions?
  1099. */
  1100. ino_t v9fs_qid2ino(struct p9_qid *qid)
  1101. {
  1102. u64 path = qid->path + 2;
  1103. ino_t i = 0;
  1104. if (sizeof(ino_t) == sizeof(path))
  1105. memcpy(&i, &path, sizeof(ino_t));
  1106. else
  1107. i = (ino_t) (path ^ (path >> 32));
  1108. return i;
  1109. }
  1110. /**
  1111. * v9fs_readlink - read a symlink's location (internal version)
  1112. * @dentry: dentry for symlink
  1113. * @buffer: buffer to load symlink location into
  1114. * @buflen: length of buffer
  1115. *
  1116. */
  1117. static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
  1118. {
  1119. int retval;
  1120. struct v9fs_session_info *v9ses;
  1121. struct p9_fid *fid;
  1122. struct p9_wstat *st;
  1123. P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
  1124. retval = -EPERM;
  1125. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  1126. fid = v9fs_fid_lookup(dentry);
  1127. if (IS_ERR(fid))
  1128. return PTR_ERR(fid);
  1129. if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses))
  1130. return -EBADF;
  1131. st = p9_client_stat(fid);
  1132. if (IS_ERR(st))
  1133. return PTR_ERR(st);
  1134. if (!(st->mode & P9_DMSYMLINK)) {
  1135. retval = -EINVAL;
  1136. goto done;
  1137. }
  1138. /* copy extension buffer into buffer */
  1139. strncpy(buffer, st->extension, buflen);
  1140. P9_DPRINTK(P9_DEBUG_VFS,
  1141. "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
  1142. retval = strnlen(buffer, buflen);
  1143. done:
  1144. kfree(st);
  1145. return retval;
  1146. }
  1147. /**
  1148. * v9fs_vfs_follow_link - follow a symlink path
  1149. * @dentry: dentry for symlink
  1150. * @nd: nameidata
  1151. *
  1152. */
  1153. static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  1154. {
  1155. int len = 0;
  1156. char *link = __getname();
  1157. P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
  1158. if (!link)
  1159. link = ERR_PTR(-ENOMEM);
  1160. else {
  1161. len = v9fs_readlink(dentry, link, PATH_MAX);
  1162. if (len < 0) {
  1163. __putname(link);
  1164. link = ERR_PTR(len);
  1165. } else
  1166. link[min(len, PATH_MAX-1)] = 0;
  1167. }
  1168. nd_set_link(nd, link);
  1169. return NULL;
  1170. }
  1171. /**
  1172. * v9fs_vfs_put_link - release a symlink path
  1173. * @dentry: dentry for symlink
  1174. * @nd: nameidata
  1175. * @p: unused
  1176. *
  1177. */
  1178. static void
  1179. v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  1180. {
  1181. char *s = nd_get_link(nd);
  1182. P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
  1183. IS_ERR(s) ? "<error>" : s);
  1184. if (!IS_ERR(s))
  1185. __putname(s);
  1186. }
  1187. /**
  1188. * v9fs_vfs_mkspecial - create a special file
  1189. * @dir: inode to create special file in
  1190. * @dentry: dentry to create
  1191. * @mode: mode to create special file
  1192. * @extension: 9p2000.u format extension string representing special file
  1193. *
  1194. */
  1195. static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
  1196. int mode, const char *extension)
  1197. {
  1198. u32 perm;
  1199. struct v9fs_session_info *v9ses;
  1200. struct p9_fid *fid;
  1201. v9ses = v9fs_inode2v9ses(dir);
  1202. if (!v9fs_proto_dotu(v9ses)) {
  1203. P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
  1204. return -EPERM;
  1205. }
  1206. perm = unixmode2p9mode(v9ses, mode);
  1207. fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
  1208. P9_OREAD);
  1209. if (IS_ERR(fid))
  1210. return PTR_ERR(fid);
  1211. p9_client_clunk(fid);
  1212. return 0;
  1213. }
  1214. /**
  1215. * v9fs_vfs_symlink_dotl - helper function to create symlinks
  1216. * @dir: directory inode containing symlink
  1217. * @dentry: dentry for symlink
  1218. * @symname: symlink data
  1219. *
  1220. * See Also: 9P2000.L RFC for more information
  1221. *
  1222. */
  1223. static int
  1224. v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
  1225. const char *symname)
  1226. {
  1227. struct v9fs_session_info *v9ses;
  1228. struct p9_fid *dfid;
  1229. struct p9_fid *fid = NULL;
  1230. struct inode *inode;
  1231. struct p9_qid qid;
  1232. char *name;
  1233. int err;
  1234. gid_t gid;
  1235. name = (char *) dentry->d_name.name;
  1236. P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n",
  1237. dir->i_ino, name, symname);
  1238. v9ses = v9fs_inode2v9ses(dir);
  1239. dfid = v9fs_fid_lookup(dentry->d_parent);
  1240. if (IS_ERR(dfid)) {
  1241. err = PTR_ERR(dfid);
  1242. P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  1243. return err;
  1244. }
  1245. gid = v9fs_get_fsgid_for_create(dir);
  1246. if (gid < 0) {
  1247. P9_DPRINTK(P9_DEBUG_VFS, "v9fs_get_egid failed %d\n", gid);
  1248. goto error;
  1249. }
  1250. /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
  1251. err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
  1252. if (err < 0) {
  1253. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
  1254. goto error;
  1255. }
  1256. if (v9ses->cache) {
  1257. /* Now walk from the parent so we can get an unopened fid. */
  1258. fid = p9_client_walk(dfid, 1, &name, 1);
  1259. if (IS_ERR(fid)) {
  1260. err = PTR_ERR(fid);
  1261. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
  1262. err);
  1263. fid = NULL;
  1264. goto error;
  1265. }
  1266. /* instantiate inode and assign the unopened fid to dentry */
  1267. inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
  1268. if (IS_ERR(inode)) {
  1269. err = PTR_ERR(inode);
  1270. P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
  1271. err);
  1272. goto error;
  1273. }
  1274. dentry->d_op = &v9fs_cached_dentry_operations;
  1275. d_instantiate(dentry, inode);
  1276. err = v9fs_fid_add(dentry, fid);
  1277. if (err < 0)
  1278. goto error;
  1279. fid = NULL;
  1280. } else {
  1281. /* Not in cached mode. No need to populate inode with stat */
  1282. inode = v9fs_get_inode(dir->i_sb, S_IFLNK);
  1283. if (IS_ERR(inode)) {
  1284. err = PTR_ERR(inode);
  1285. goto error;
  1286. }
  1287. dentry->d_op = &v9fs_dentry_operations;
  1288. d_instantiate(dentry, inode);
  1289. }
  1290. error:
  1291. if (fid)
  1292. p9_client_clunk(fid);
  1293. return err;
  1294. }
  1295. /**
  1296. * v9fs_vfs_symlink - helper function to create symlinks
  1297. * @dir: directory inode containing symlink
  1298. * @dentry: dentry for symlink
  1299. * @symname: symlink data
  1300. *
  1301. * See Also: 9P2000.u RFC for more information
  1302. *
  1303. */
  1304. static int
  1305. v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1306. {
  1307. P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
  1308. dentry->d_name.name, symname);
  1309. return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
  1310. }
  1311. /**
  1312. * v9fs_vfs_link - create a hardlink
  1313. * @old_dentry: dentry for file to link to
  1314. * @dir: inode destination for new link
  1315. * @dentry: dentry for link
  1316. *
  1317. */
  1318. static int
  1319. v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
  1320. struct dentry *dentry)
  1321. {
  1322. int retval;
  1323. struct p9_fid *oldfid;
  1324. char *name;
  1325. P9_DPRINTK(P9_DEBUG_VFS,
  1326. " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
  1327. old_dentry->d_name.name);
  1328. oldfid = v9fs_fid_clone(old_dentry);
  1329. if (IS_ERR(oldfid))
  1330. return PTR_ERR(oldfid);
  1331. name = __getname();
  1332. if (unlikely(!name)) {
  1333. retval = -ENOMEM;
  1334. goto clunk_fid;
  1335. }
  1336. sprintf(name, "%d\n", oldfid->fid);
  1337. retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
  1338. __putname(name);
  1339. clunk_fid:
  1340. p9_client_clunk(oldfid);
  1341. return retval;
  1342. }
  1343. /**
  1344. * v9fs_vfs_link_dotl - create a hardlink for dotl
  1345. * @old_dentry: dentry for file to link to
  1346. * @dir: inode destination for new link
  1347. * @dentry: dentry for link
  1348. *
  1349. */
  1350. static int
  1351. v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
  1352. struct dentry *dentry)
  1353. {
  1354. int err;
  1355. struct p9_fid *dfid, *oldfid;
  1356. char *name;
  1357. struct v9fs_session_info *v9ses;
  1358. struct dentry *dir_dentry;
  1359. P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
  1360. dir->i_ino, old_dentry->d_name.name,
  1361. dentry->d_name.name);
  1362. v9ses = v9fs_inode2v9ses(dir);
  1363. dir_dentry = v9fs_dentry_from_dir_inode(dir);
  1364. dfid = v9fs_fid_lookup(dir_dentry);
  1365. if (IS_ERR(dfid))
  1366. return PTR_ERR(dfid);
  1367. oldfid = v9fs_fid_lookup(old_dentry);
  1368. if (IS_ERR(oldfid))
  1369. return PTR_ERR(oldfid);
  1370. name = (char *) dentry->d_name.name;
  1371. err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
  1372. if (err < 0) {
  1373. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
  1374. return err;
  1375. }
  1376. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  1377. /* Get the latest stat info from server. */
  1378. struct p9_fid *fid;
  1379. struct p9_stat_dotl *st;
  1380. fid = v9fs_fid_lookup(old_dentry);
  1381. if (IS_ERR(fid))
  1382. return PTR_ERR(fid);
  1383. st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
  1384. if (IS_ERR(st))
  1385. return PTR_ERR(st);
  1386. v9fs_stat2inode_dotl(st, old_dentry->d_inode);
  1387. kfree(st);
  1388. } else {
  1389. /* Caching disabled. No need to get upto date stat info.
  1390. * This dentry will be released immediately. So, just i_count++
  1391. */
  1392. atomic_inc(&old_dentry->d_inode->i_count);
  1393. }
  1394. dentry->d_op = old_dentry->d_op;
  1395. d_instantiate(dentry, old_dentry->d_inode);
  1396. return err;
  1397. }
  1398. /**
  1399. * v9fs_vfs_mknod - create a special file
  1400. * @dir: inode destination for new link
  1401. * @dentry: dentry for file
  1402. * @mode: mode for creation
  1403. * @rdev: device associated with special file
  1404. *
  1405. */
  1406. static int
  1407. v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1408. {
  1409. int retval;
  1410. char *name;
  1411. P9_DPRINTK(P9_DEBUG_VFS,
  1412. " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
  1413. dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
  1414. if (!new_valid_dev(rdev))
  1415. return -EINVAL;
  1416. name = __getname();
  1417. if (!name)
  1418. return -ENOMEM;
  1419. /* build extension */
  1420. if (S_ISBLK(mode))
  1421. sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
  1422. else if (S_ISCHR(mode))
  1423. sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
  1424. else if (S_ISFIFO(mode))
  1425. *name = 0;
  1426. else if (S_ISSOCK(mode))
  1427. *name = 0;
  1428. else {
  1429. __putname(name);
  1430. return -EINVAL;
  1431. }
  1432. retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
  1433. __putname(name);
  1434. return retval;
  1435. }
  1436. /**
  1437. * v9fs_vfs_mknod_dotl - create a special file
  1438. * @dir: inode destination for new link
  1439. * @dentry: dentry for file
  1440. * @mode: mode for creation
  1441. * @rdev: device associated with special file
  1442. *
  1443. */
  1444. static int
  1445. v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int mode,
  1446. dev_t rdev)
  1447. {
  1448. int err;
  1449. char *name;
  1450. struct v9fs_session_info *v9ses;
  1451. struct p9_fid *fid = NULL, *dfid = NULL;
  1452. struct inode *inode;
  1453. gid_t gid;
  1454. struct p9_qid qid;
  1455. struct dentry *dir_dentry;
  1456. P9_DPRINTK(P9_DEBUG_VFS,
  1457. " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
  1458. dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
  1459. if (!new_valid_dev(rdev))
  1460. return -EINVAL;
  1461. v9ses = v9fs_inode2v9ses(dir);
  1462. dir_dentry = v9fs_dentry_from_dir_inode(dir);
  1463. dfid = v9fs_fid_lookup(dir_dentry);
  1464. if (IS_ERR(dfid)) {
  1465. err = PTR_ERR(dfid);
  1466. P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  1467. dfid = NULL;
  1468. goto error;
  1469. }
  1470. gid = v9fs_get_fsgid_for_create(dir);
  1471. if (gid < 0) {
  1472. P9_DPRINTK(P9_DEBUG_VFS, "v9fs_get_fsgid_for_create failed\n");
  1473. goto error;
  1474. }
  1475. name = (char *) dentry->d_name.name;
  1476. err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
  1477. if (err < 0)
  1478. goto error;
  1479. /* instantiate inode and assign the unopened fid to the dentry */
  1480. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  1481. fid = p9_client_walk(dfid, 1, &name, 1);
  1482. if (IS_ERR(fid)) {
  1483. err = PTR_ERR(fid);
  1484. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
  1485. err);
  1486. fid = NULL;
  1487. goto error;
  1488. }
  1489. inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
  1490. if (IS_ERR(inode)) {
  1491. err = PTR_ERR(inode);
  1492. P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
  1493. err);
  1494. goto error;
  1495. }
  1496. dentry->d_op = &v9fs_cached_dentry_operations;
  1497. d_instantiate(dentry, inode);
  1498. err = v9fs_fid_add(dentry, fid);
  1499. if (err < 0)
  1500. goto error;
  1501. fid = NULL;
  1502. } else {
  1503. /*
  1504. * Not in cached mode. No need to populate inode with stat.
  1505. * socket syscall returns a fd, so we need instantiate
  1506. */
  1507. inode = v9fs_get_inode(dir->i_sb, mode);
  1508. if (IS_ERR(inode)) {
  1509. err = PTR_ERR(inode);
  1510. goto error;
  1511. }
  1512. dentry->d_op = &v9fs_dentry_operations;
  1513. d_instantiate(dentry, inode);
  1514. }
  1515. error:
  1516. if (fid)
  1517. p9_client_clunk(fid);
  1518. return err;
  1519. }
  1520. static const struct inode_operations v9fs_dir_inode_operations_dotu = {
  1521. .create = v9fs_vfs_create,
  1522. .lookup = v9fs_vfs_lookup,
  1523. .symlink = v9fs_vfs_symlink,
  1524. .link = v9fs_vfs_link,
  1525. .unlink = v9fs_vfs_unlink,
  1526. .mkdir = v9fs_vfs_mkdir,
  1527. .rmdir = v9fs_vfs_rmdir,
  1528. .mknod = v9fs_vfs_mknod_dotl,
  1529. .rename = v9fs_vfs_rename,
  1530. .getattr = v9fs_vfs_getattr,
  1531. .setattr = v9fs_vfs_setattr,
  1532. };
  1533. static const struct inode_operations v9fs_dir_inode_operations_dotl = {
  1534. .create = v9fs_vfs_create,
  1535. .lookup = v9fs_vfs_lookup,
  1536. .link = v9fs_vfs_link_dotl,
  1537. .symlink = v9fs_vfs_symlink_dotl,
  1538. .unlink = v9fs_vfs_unlink,
  1539. .mkdir = v9fs_vfs_mkdir_dotl,
  1540. .rmdir = v9fs_vfs_rmdir,
  1541. .mknod = v9fs_vfs_mknod_dotl,
  1542. .rename = v9fs_vfs_rename,
  1543. .getattr = v9fs_vfs_getattr_dotl,
  1544. .setattr = v9fs_vfs_setattr_dotl,
  1545. };
  1546. static const struct inode_operations v9fs_dir_inode_operations = {
  1547. .create = v9fs_vfs_create,
  1548. .lookup = v9fs_vfs_lookup,
  1549. .unlink = v9fs_vfs_unlink,
  1550. .mkdir = v9fs_vfs_mkdir,
  1551. .rmdir = v9fs_vfs_rmdir,
  1552. .mknod = v9fs_vfs_mknod,
  1553. .rename = v9fs_vfs_rename,
  1554. .getattr = v9fs_vfs_getattr,
  1555. .setattr = v9fs_vfs_setattr,
  1556. };
  1557. static const struct inode_operations v9fs_file_inode_operations = {
  1558. .getattr = v9fs_vfs_getattr,
  1559. .setattr = v9fs_vfs_setattr,
  1560. };
  1561. static const struct inode_operations v9fs_file_inode_operations_dotl = {
  1562. .getattr = v9fs_vfs_getattr_dotl,
  1563. .setattr = v9fs_vfs_setattr_dotl,
  1564. };
  1565. static const struct inode_operations v9fs_symlink_inode_operations = {
  1566. .readlink = generic_readlink,
  1567. .follow_link = v9fs_vfs_follow_link,
  1568. .put_link = v9fs_vfs_put_link,
  1569. .getattr = v9fs_vfs_getattr,
  1570. .setattr = v9fs_vfs_setattr,
  1571. };
  1572. static const struct inode_operations v9fs_symlink_inode_operations_dotl = {
  1573. .readlink = generic_readlink,
  1574. .follow_link = v9fs_vfs_follow_link,
  1575. .put_link = v9fs_vfs_put_link,
  1576. .getattr = v9fs_vfs_getattr_dotl,
  1577. .setattr = v9fs_vfs_setattr_dotl,
  1578. };