vfs_inode.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  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_lookup - VFS lookup hook to "walk" to a new inode
  627. * @dir: inode that is being walked from
  628. * @dentry: dentry that is being walked to?
  629. * @nameidata: path data
  630. *
  631. */
  632. static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
  633. struct nameidata *nameidata)
  634. {
  635. struct super_block *sb;
  636. struct v9fs_session_info *v9ses;
  637. struct p9_fid *dfid, *fid;
  638. struct inode *inode;
  639. char *name;
  640. int result = 0;
  641. P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
  642. dir, dentry->d_name.name, dentry, nameidata);
  643. if (dentry->d_name.len > NAME_MAX)
  644. return ERR_PTR(-ENAMETOOLONG);
  645. sb = dir->i_sb;
  646. v9ses = v9fs_inode2v9ses(dir);
  647. dfid = v9fs_fid_lookup(dentry->d_parent);
  648. if (IS_ERR(dfid))
  649. return ERR_CAST(dfid);
  650. name = (char *) dentry->d_name.name;
  651. fid = p9_client_walk(dfid, 1, &name, 1);
  652. if (IS_ERR(fid)) {
  653. result = PTR_ERR(fid);
  654. if (result == -ENOENT) {
  655. inode = NULL;
  656. goto inst_out;
  657. }
  658. return ERR_PTR(result);
  659. }
  660. inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
  661. if (IS_ERR(inode)) {
  662. result = PTR_ERR(inode);
  663. inode = NULL;
  664. goto error;
  665. }
  666. result = v9fs_fid_add(dentry, fid);
  667. if (result < 0)
  668. goto error;
  669. inst_out:
  670. if (v9ses->cache)
  671. dentry->d_op = &v9fs_cached_dentry_operations;
  672. else
  673. dentry->d_op = &v9fs_dentry_operations;
  674. d_add(dentry, inode);
  675. return NULL;
  676. error:
  677. p9_client_clunk(fid);
  678. return ERR_PTR(result);
  679. }
  680. /**
  681. * v9fs_vfs_unlink - VFS unlink hook to delete an inode
  682. * @i: inode that is being unlinked
  683. * @d: dentry that is being unlinked
  684. *
  685. */
  686. static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
  687. {
  688. return v9fs_remove(i, d, 0);
  689. }
  690. /**
  691. * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
  692. * @i: inode that is being unlinked
  693. * @d: dentry that is being unlinked
  694. *
  695. */
  696. static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
  697. {
  698. return v9fs_remove(i, d, 1);
  699. }
  700. /**
  701. * v9fs_vfs_rename - VFS hook to rename an inode
  702. * @old_dir: old dir inode
  703. * @old_dentry: old dentry
  704. * @new_dir: new dir inode
  705. * @new_dentry: new dentry
  706. *
  707. */
  708. static int
  709. v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  710. struct inode *new_dir, struct dentry *new_dentry)
  711. {
  712. struct inode *old_inode;
  713. struct v9fs_session_info *v9ses;
  714. struct p9_fid *oldfid;
  715. struct p9_fid *olddirfid;
  716. struct p9_fid *newdirfid;
  717. struct p9_wstat wstat;
  718. int retval;
  719. P9_DPRINTK(P9_DEBUG_VFS, "\n");
  720. retval = 0;
  721. old_inode = old_dentry->d_inode;
  722. v9ses = v9fs_inode2v9ses(old_inode);
  723. oldfid = v9fs_fid_lookup(old_dentry);
  724. if (IS_ERR(oldfid))
  725. return PTR_ERR(oldfid);
  726. olddirfid = v9fs_fid_clone(old_dentry->d_parent);
  727. if (IS_ERR(olddirfid)) {
  728. retval = PTR_ERR(olddirfid);
  729. goto done;
  730. }
  731. newdirfid = v9fs_fid_clone(new_dentry->d_parent);
  732. if (IS_ERR(newdirfid)) {
  733. retval = PTR_ERR(newdirfid);
  734. goto clunk_olddir;
  735. }
  736. if (v9fs_proto_dotl(v9ses)) {
  737. retval = p9_client_rename(oldfid, newdirfid,
  738. (char *) new_dentry->d_name.name);
  739. if (retval != -ENOSYS)
  740. goto clunk_newdir;
  741. }
  742. /* 9P can only handle file rename in the same directory */
  743. if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
  744. P9_DPRINTK(P9_DEBUG_ERROR,
  745. "old dir and new dir are different\n");
  746. retval = -EXDEV;
  747. goto clunk_newdir;
  748. }
  749. v9fs_blank_wstat(&wstat);
  750. wstat.muid = v9ses->uname;
  751. wstat.name = (char *) new_dentry->d_name.name;
  752. retval = p9_client_wstat(oldfid, &wstat);
  753. clunk_newdir:
  754. p9_client_clunk(newdirfid);
  755. clunk_olddir:
  756. p9_client_clunk(olddirfid);
  757. done:
  758. return retval;
  759. }
  760. /**
  761. * v9fs_vfs_getattr - retrieve file metadata
  762. * @mnt: mount information
  763. * @dentry: file to get attributes on
  764. * @stat: metadata structure to populate
  765. *
  766. */
  767. static int
  768. v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  769. struct kstat *stat)
  770. {
  771. int err;
  772. struct v9fs_session_info *v9ses;
  773. struct p9_fid *fid;
  774. struct p9_wstat *st;
  775. P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
  776. err = -EPERM;
  777. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  778. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  779. return simple_getattr(mnt, dentry, stat);
  780. fid = v9fs_fid_lookup(dentry);
  781. if (IS_ERR(fid))
  782. return PTR_ERR(fid);
  783. st = p9_client_stat(fid);
  784. if (IS_ERR(st))
  785. return PTR_ERR(st);
  786. v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
  787. generic_fillattr(dentry->d_inode, stat);
  788. kfree(st);
  789. return 0;
  790. }
  791. static int
  792. v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
  793. struct kstat *stat)
  794. {
  795. int err;
  796. struct v9fs_session_info *v9ses;
  797. struct p9_fid *fid;
  798. struct p9_stat_dotl *st;
  799. P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
  800. err = -EPERM;
  801. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  802. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  803. return simple_getattr(mnt, dentry, stat);
  804. fid = v9fs_fid_lookup(dentry);
  805. if (IS_ERR(fid))
  806. return PTR_ERR(fid);
  807. /* Ask for all the fields in stat structure. Server will return
  808. * whatever it supports
  809. */
  810. st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
  811. if (IS_ERR(st))
  812. return PTR_ERR(st);
  813. v9fs_stat2inode_dotl(st, dentry->d_inode);
  814. generic_fillattr(dentry->d_inode, stat);
  815. /* Change block size to what the server returned */
  816. stat->blksize = st->st_blksize;
  817. kfree(st);
  818. return 0;
  819. }
  820. /**
  821. * v9fs_vfs_setattr - set file metadata
  822. * @dentry: file whose metadata to set
  823. * @iattr: metadata assignment structure
  824. *
  825. */
  826. static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
  827. {
  828. int retval;
  829. struct v9fs_session_info *v9ses;
  830. struct p9_fid *fid;
  831. struct p9_wstat wstat;
  832. P9_DPRINTK(P9_DEBUG_VFS, "\n");
  833. retval = -EPERM;
  834. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  835. fid = v9fs_fid_lookup(dentry);
  836. if(IS_ERR(fid))
  837. return PTR_ERR(fid);
  838. v9fs_blank_wstat(&wstat);
  839. if (iattr->ia_valid & ATTR_MODE)
  840. wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
  841. if (iattr->ia_valid & ATTR_MTIME)
  842. wstat.mtime = iattr->ia_mtime.tv_sec;
  843. if (iattr->ia_valid & ATTR_ATIME)
  844. wstat.atime = iattr->ia_atime.tv_sec;
  845. if (iattr->ia_valid & ATTR_SIZE)
  846. wstat.length = iattr->ia_size;
  847. if (v9fs_proto_dotu(v9ses)) {
  848. if (iattr->ia_valid & ATTR_UID)
  849. wstat.n_uid = iattr->ia_uid;
  850. if (iattr->ia_valid & ATTR_GID)
  851. wstat.n_gid = iattr->ia_gid;
  852. }
  853. retval = p9_client_wstat(fid, &wstat);
  854. if (retval >= 0)
  855. retval = inode_setattr(dentry->d_inode, iattr);
  856. return retval;
  857. }
  858. /**
  859. * v9fs_vfs_setattr_dotl - set file metadata
  860. * @dentry: file whose metadata to set
  861. * @iattr: metadata assignment structure
  862. *
  863. */
  864. static int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
  865. {
  866. int retval;
  867. struct v9fs_session_info *v9ses;
  868. struct p9_fid *fid;
  869. struct p9_iattr_dotl p9attr;
  870. P9_DPRINTK(P9_DEBUG_VFS, "\n");
  871. retval = inode_change_ok(dentry->d_inode, iattr);
  872. if (retval)
  873. return retval;
  874. p9attr.valid = iattr->ia_valid;
  875. p9attr.mode = iattr->ia_mode;
  876. p9attr.uid = iattr->ia_uid;
  877. p9attr.gid = iattr->ia_gid;
  878. p9attr.size = iattr->ia_size;
  879. p9attr.atime_sec = iattr->ia_atime.tv_sec;
  880. p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
  881. p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
  882. p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
  883. retval = -EPERM;
  884. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  885. fid = v9fs_fid_lookup(dentry);
  886. if (IS_ERR(fid))
  887. return PTR_ERR(fid);
  888. retval = p9_client_setattr(fid, &p9attr);
  889. if (retval >= 0)
  890. retval = inode_setattr(dentry->d_inode, iattr);
  891. return retval;
  892. }
  893. /**
  894. * v9fs_stat2inode - populate an inode structure with mistat info
  895. * @stat: Plan 9 metadata (mistat) structure
  896. * @inode: inode to populate
  897. * @sb: superblock of filesystem
  898. *
  899. */
  900. void
  901. v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
  902. struct super_block *sb)
  903. {
  904. char ext[32];
  905. char tag_name[14];
  906. unsigned int i_nlink;
  907. struct v9fs_session_info *v9ses = sb->s_fs_info;
  908. inode->i_nlink = 1;
  909. inode->i_atime.tv_sec = stat->atime;
  910. inode->i_mtime.tv_sec = stat->mtime;
  911. inode->i_ctime.tv_sec = stat->mtime;
  912. inode->i_uid = v9ses->dfltuid;
  913. inode->i_gid = v9ses->dfltgid;
  914. if (v9fs_proto_dotu(v9ses)) {
  915. inode->i_uid = stat->n_uid;
  916. inode->i_gid = stat->n_gid;
  917. }
  918. if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
  919. if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
  920. /*
  921. * Hadlink support got added later to
  922. * to the .u extension. So there can be
  923. * server out there that doesn't support
  924. * this even with .u extension. So check
  925. * for non NULL stat->extension
  926. */
  927. strncpy(ext, stat->extension, sizeof(ext));
  928. /* HARDLINKCOUNT %u */
  929. sscanf(ext, "%13s %u", tag_name, &i_nlink);
  930. if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
  931. inode->i_nlink = i_nlink;
  932. }
  933. }
  934. inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
  935. if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
  936. char type = 0;
  937. int major = -1;
  938. int minor = -1;
  939. strncpy(ext, stat->extension, sizeof(ext));
  940. sscanf(ext, "%c %u %u", &type, &major, &minor);
  941. switch (type) {
  942. case 'c':
  943. inode->i_mode &= ~S_IFBLK;
  944. inode->i_mode |= S_IFCHR;
  945. break;
  946. case 'b':
  947. break;
  948. default:
  949. P9_DPRINTK(P9_DEBUG_ERROR,
  950. "Unknown special type %c %s\n", type,
  951. stat->extension);
  952. };
  953. inode->i_rdev = MKDEV(major, minor);
  954. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  955. } else
  956. inode->i_rdev = 0;
  957. i_size_write(inode, stat->length);
  958. /* not real number of blocks, but 512 byte ones ... */
  959. inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
  960. }
  961. /**
  962. * v9fs_stat2inode_dotl - populate an inode structure with stat info
  963. * @stat: stat structure
  964. * @inode: inode to populate
  965. * @sb: superblock of filesystem
  966. *
  967. */
  968. void
  969. v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
  970. {
  971. if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
  972. inode->i_atime.tv_sec = stat->st_atime_sec;
  973. inode->i_atime.tv_nsec = stat->st_atime_nsec;
  974. inode->i_mtime.tv_sec = stat->st_mtime_sec;
  975. inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
  976. inode->i_ctime.tv_sec = stat->st_ctime_sec;
  977. inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
  978. inode->i_uid = stat->st_uid;
  979. inode->i_gid = stat->st_gid;
  980. inode->i_nlink = stat->st_nlink;
  981. inode->i_mode = stat->st_mode;
  982. inode->i_rdev = new_decode_dev(stat->st_rdev);
  983. if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode)))
  984. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  985. i_size_write(inode, stat->st_size);
  986. inode->i_blocks = stat->st_blocks;
  987. } else {
  988. if (stat->st_result_mask & P9_STATS_ATIME) {
  989. inode->i_atime.tv_sec = stat->st_atime_sec;
  990. inode->i_atime.tv_nsec = stat->st_atime_nsec;
  991. }
  992. if (stat->st_result_mask & P9_STATS_MTIME) {
  993. inode->i_mtime.tv_sec = stat->st_mtime_sec;
  994. inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
  995. }
  996. if (stat->st_result_mask & P9_STATS_CTIME) {
  997. inode->i_ctime.tv_sec = stat->st_ctime_sec;
  998. inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
  999. }
  1000. if (stat->st_result_mask & P9_STATS_UID)
  1001. inode->i_uid = stat->st_uid;
  1002. if (stat->st_result_mask & P9_STATS_GID)
  1003. inode->i_gid = stat->st_gid;
  1004. if (stat->st_result_mask & P9_STATS_NLINK)
  1005. inode->i_nlink = stat->st_nlink;
  1006. if (stat->st_result_mask & P9_STATS_MODE) {
  1007. inode->i_mode = stat->st_mode;
  1008. if ((S_ISBLK(inode->i_mode)) ||
  1009. (S_ISCHR(inode->i_mode)))
  1010. init_special_inode(inode, inode->i_mode,
  1011. inode->i_rdev);
  1012. }
  1013. if (stat->st_result_mask & P9_STATS_RDEV)
  1014. inode->i_rdev = new_decode_dev(stat->st_rdev);
  1015. if (stat->st_result_mask & P9_STATS_SIZE)
  1016. i_size_write(inode, stat->st_size);
  1017. if (stat->st_result_mask & P9_STATS_BLOCKS)
  1018. inode->i_blocks = stat->st_blocks;
  1019. }
  1020. if (stat->st_result_mask & P9_STATS_GEN)
  1021. inode->i_generation = stat->st_gen;
  1022. /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
  1023. * because the inode structure does not have fields for them.
  1024. */
  1025. }
  1026. /**
  1027. * v9fs_qid2ino - convert qid into inode number
  1028. * @qid: qid to hash
  1029. *
  1030. * BUG: potential for inode number collisions?
  1031. */
  1032. ino_t v9fs_qid2ino(struct p9_qid *qid)
  1033. {
  1034. u64 path = qid->path + 2;
  1035. ino_t i = 0;
  1036. if (sizeof(ino_t) == sizeof(path))
  1037. memcpy(&i, &path, sizeof(ino_t));
  1038. else
  1039. i = (ino_t) (path ^ (path >> 32));
  1040. return i;
  1041. }
  1042. /**
  1043. * v9fs_readlink - read a symlink's location (internal version)
  1044. * @dentry: dentry for symlink
  1045. * @buffer: buffer to load symlink location into
  1046. * @buflen: length of buffer
  1047. *
  1048. */
  1049. static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
  1050. {
  1051. int retval;
  1052. struct v9fs_session_info *v9ses;
  1053. struct p9_fid *fid;
  1054. struct p9_wstat *st;
  1055. P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
  1056. retval = -EPERM;
  1057. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  1058. fid = v9fs_fid_lookup(dentry);
  1059. if (IS_ERR(fid))
  1060. return PTR_ERR(fid);
  1061. if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses))
  1062. return -EBADF;
  1063. st = p9_client_stat(fid);
  1064. if (IS_ERR(st))
  1065. return PTR_ERR(st);
  1066. if (!(st->mode & P9_DMSYMLINK)) {
  1067. retval = -EINVAL;
  1068. goto done;
  1069. }
  1070. /* copy extension buffer into buffer */
  1071. strncpy(buffer, st->extension, buflen);
  1072. P9_DPRINTK(P9_DEBUG_VFS,
  1073. "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
  1074. retval = strnlen(buffer, buflen);
  1075. done:
  1076. kfree(st);
  1077. return retval;
  1078. }
  1079. /**
  1080. * v9fs_vfs_follow_link - follow a symlink path
  1081. * @dentry: dentry for symlink
  1082. * @nd: nameidata
  1083. *
  1084. */
  1085. static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  1086. {
  1087. int len = 0;
  1088. char *link = __getname();
  1089. P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
  1090. if (!link)
  1091. link = ERR_PTR(-ENOMEM);
  1092. else {
  1093. len = v9fs_readlink(dentry, link, PATH_MAX);
  1094. if (len < 0) {
  1095. __putname(link);
  1096. link = ERR_PTR(len);
  1097. } else
  1098. link[min(len, PATH_MAX-1)] = 0;
  1099. }
  1100. nd_set_link(nd, link);
  1101. return NULL;
  1102. }
  1103. /**
  1104. * v9fs_vfs_put_link - release a symlink path
  1105. * @dentry: dentry for symlink
  1106. * @nd: nameidata
  1107. * @p: unused
  1108. *
  1109. */
  1110. static void
  1111. v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  1112. {
  1113. char *s = nd_get_link(nd);
  1114. P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
  1115. IS_ERR(s) ? "<error>" : s);
  1116. if (!IS_ERR(s))
  1117. __putname(s);
  1118. }
  1119. /**
  1120. * v9fs_vfs_mkspecial - create a special file
  1121. * @dir: inode to create special file in
  1122. * @dentry: dentry to create
  1123. * @mode: mode to create special file
  1124. * @extension: 9p2000.u format extension string representing special file
  1125. *
  1126. */
  1127. static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
  1128. int mode, const char *extension)
  1129. {
  1130. u32 perm;
  1131. struct v9fs_session_info *v9ses;
  1132. struct p9_fid *fid;
  1133. v9ses = v9fs_inode2v9ses(dir);
  1134. if (!v9fs_proto_dotu(v9ses)) {
  1135. P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
  1136. return -EPERM;
  1137. }
  1138. perm = unixmode2p9mode(v9ses, mode);
  1139. fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
  1140. P9_OREAD);
  1141. if (IS_ERR(fid))
  1142. return PTR_ERR(fid);
  1143. p9_client_clunk(fid);
  1144. return 0;
  1145. }
  1146. /**
  1147. * v9fs_vfs_symlink_dotl - helper function to create symlinks
  1148. * @dir: directory inode containing symlink
  1149. * @dentry: dentry for symlink
  1150. * @symname: symlink data
  1151. *
  1152. * See Also: 9P2000.L RFC for more information
  1153. *
  1154. */
  1155. static int
  1156. v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
  1157. const char *symname)
  1158. {
  1159. struct v9fs_session_info *v9ses;
  1160. struct p9_fid *dfid;
  1161. struct p9_fid *fid = NULL;
  1162. struct inode *inode;
  1163. struct p9_qid qid;
  1164. char *name;
  1165. int err;
  1166. gid_t gid;
  1167. name = (char *) dentry->d_name.name;
  1168. P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n",
  1169. dir->i_ino, name, symname);
  1170. v9ses = v9fs_inode2v9ses(dir);
  1171. dfid = v9fs_fid_lookup(dentry->d_parent);
  1172. if (IS_ERR(dfid)) {
  1173. err = PTR_ERR(dfid);
  1174. P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  1175. return err;
  1176. }
  1177. gid = v9fs_get_fsgid_for_create(dir);
  1178. if (gid < 0) {
  1179. P9_DPRINTK(P9_DEBUG_VFS, "v9fs_get_egid failed %d\n", gid);
  1180. goto error;
  1181. }
  1182. /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
  1183. err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
  1184. if (err < 0) {
  1185. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
  1186. goto error;
  1187. }
  1188. if (v9ses->cache) {
  1189. /* Now walk from the parent so we can get an unopened fid. */
  1190. fid = p9_client_walk(dfid, 1, &name, 1);
  1191. if (IS_ERR(fid)) {
  1192. err = PTR_ERR(fid);
  1193. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
  1194. err);
  1195. fid = NULL;
  1196. goto error;
  1197. }
  1198. /* instantiate inode and assign the unopened fid to dentry */
  1199. inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
  1200. if (IS_ERR(inode)) {
  1201. err = PTR_ERR(inode);
  1202. P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
  1203. err);
  1204. goto error;
  1205. }
  1206. dentry->d_op = &v9fs_cached_dentry_operations;
  1207. d_instantiate(dentry, inode);
  1208. err = v9fs_fid_add(dentry, fid);
  1209. if (err < 0)
  1210. goto error;
  1211. fid = NULL;
  1212. } else {
  1213. /* Not in cached mode. No need to populate inode with stat */
  1214. inode = v9fs_get_inode(dir->i_sb, S_IFLNK);
  1215. if (IS_ERR(inode)) {
  1216. err = PTR_ERR(inode);
  1217. goto error;
  1218. }
  1219. dentry->d_op = &v9fs_dentry_operations;
  1220. d_instantiate(dentry, inode);
  1221. }
  1222. error:
  1223. if (fid)
  1224. p9_client_clunk(fid);
  1225. return err;
  1226. }
  1227. /**
  1228. * v9fs_vfs_symlink - helper function to create symlinks
  1229. * @dir: directory inode containing symlink
  1230. * @dentry: dentry for symlink
  1231. * @symname: symlink data
  1232. *
  1233. * See Also: 9P2000.u RFC for more information
  1234. *
  1235. */
  1236. static int
  1237. v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1238. {
  1239. P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
  1240. dentry->d_name.name, symname);
  1241. return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
  1242. }
  1243. /**
  1244. * v9fs_vfs_link - create a hardlink
  1245. * @old_dentry: dentry for file to link to
  1246. * @dir: inode destination for new link
  1247. * @dentry: dentry for link
  1248. *
  1249. */
  1250. static int
  1251. v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
  1252. struct dentry *dentry)
  1253. {
  1254. int retval;
  1255. struct p9_fid *oldfid;
  1256. char *name;
  1257. P9_DPRINTK(P9_DEBUG_VFS,
  1258. " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
  1259. old_dentry->d_name.name);
  1260. oldfid = v9fs_fid_clone(old_dentry);
  1261. if (IS_ERR(oldfid))
  1262. return PTR_ERR(oldfid);
  1263. name = __getname();
  1264. if (unlikely(!name)) {
  1265. retval = -ENOMEM;
  1266. goto clunk_fid;
  1267. }
  1268. sprintf(name, "%d\n", oldfid->fid);
  1269. retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
  1270. __putname(name);
  1271. clunk_fid:
  1272. p9_client_clunk(oldfid);
  1273. return retval;
  1274. }
  1275. /**
  1276. * v9fs_vfs_link_dotl - create a hardlink for dotl
  1277. * @old_dentry: dentry for file to link to
  1278. * @dir: inode destination for new link
  1279. * @dentry: dentry for link
  1280. *
  1281. */
  1282. static int
  1283. v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
  1284. struct dentry *dentry)
  1285. {
  1286. int err;
  1287. struct p9_fid *dfid, *oldfid;
  1288. char *name;
  1289. struct v9fs_session_info *v9ses;
  1290. struct dentry *dir_dentry;
  1291. P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
  1292. dir->i_ino, old_dentry->d_name.name,
  1293. dentry->d_name.name);
  1294. v9ses = v9fs_inode2v9ses(dir);
  1295. dir_dentry = v9fs_dentry_from_dir_inode(dir);
  1296. dfid = v9fs_fid_lookup(dir_dentry);
  1297. if (IS_ERR(dfid))
  1298. return PTR_ERR(dfid);
  1299. oldfid = v9fs_fid_lookup(old_dentry);
  1300. if (IS_ERR(oldfid))
  1301. return PTR_ERR(oldfid);
  1302. name = (char *) dentry->d_name.name;
  1303. err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
  1304. if (err < 0) {
  1305. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
  1306. return err;
  1307. }
  1308. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  1309. /* Get the latest stat info from server. */
  1310. struct p9_fid *fid;
  1311. struct p9_stat_dotl *st;
  1312. fid = v9fs_fid_lookup(old_dentry);
  1313. if (IS_ERR(fid))
  1314. return PTR_ERR(fid);
  1315. st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
  1316. if (IS_ERR(st))
  1317. return PTR_ERR(st);
  1318. v9fs_stat2inode_dotl(st, old_dentry->d_inode);
  1319. kfree(st);
  1320. } else {
  1321. /* Caching disabled. No need to get upto date stat info.
  1322. * This dentry will be released immediately. So, just i_count++
  1323. */
  1324. atomic_inc(&old_dentry->d_inode->i_count);
  1325. }
  1326. dentry->d_op = old_dentry->d_op;
  1327. d_instantiate(dentry, old_dentry->d_inode);
  1328. return err;
  1329. }
  1330. /**
  1331. * v9fs_vfs_mknod - create a special file
  1332. * @dir: inode destination for new link
  1333. * @dentry: dentry for file
  1334. * @mode: mode for creation
  1335. * @rdev: device associated with special file
  1336. *
  1337. */
  1338. static int
  1339. v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1340. {
  1341. int retval;
  1342. char *name;
  1343. P9_DPRINTK(P9_DEBUG_VFS,
  1344. " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
  1345. dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
  1346. if (!new_valid_dev(rdev))
  1347. return -EINVAL;
  1348. name = __getname();
  1349. if (!name)
  1350. return -ENOMEM;
  1351. /* build extension */
  1352. if (S_ISBLK(mode))
  1353. sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
  1354. else if (S_ISCHR(mode))
  1355. sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
  1356. else if (S_ISFIFO(mode))
  1357. *name = 0;
  1358. else if (S_ISSOCK(mode))
  1359. *name = 0;
  1360. else {
  1361. __putname(name);
  1362. return -EINVAL;
  1363. }
  1364. retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
  1365. __putname(name);
  1366. return retval;
  1367. }
  1368. /**
  1369. * v9fs_vfs_mknod_dotl - create a special file
  1370. * @dir: inode destination for new link
  1371. * @dentry: dentry for file
  1372. * @mode: mode for creation
  1373. * @rdev: device associated with special file
  1374. *
  1375. */
  1376. static int
  1377. v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int mode,
  1378. dev_t rdev)
  1379. {
  1380. int err;
  1381. char *name;
  1382. struct v9fs_session_info *v9ses;
  1383. struct p9_fid *fid = NULL, *dfid = NULL;
  1384. struct inode *inode;
  1385. gid_t gid;
  1386. struct p9_qid qid;
  1387. struct dentry *dir_entry;
  1388. P9_DPRINTK(P9_DEBUG_VFS,
  1389. " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
  1390. dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
  1391. if (!new_valid_dev(rdev))
  1392. return -EINVAL;
  1393. v9ses = v9fs_inode2v9ses(dir);
  1394. dir_dentry = v9fs_dentry_from_dir_inode(dir);
  1395. dfid = v9fs_fid_lookup(dir_entry);
  1396. if (IS_ERR(dfid)) {
  1397. err = PTR_ERR(dfid);
  1398. P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  1399. dfid = NULL;
  1400. goto error;
  1401. }
  1402. gid = v9fs_get_fsgid_for_create(dir);
  1403. if (gid < 0) {
  1404. P9_DPRINTK(P9_DEBUG_VFS, "v9fs_get_fsgid_for_create failed\n");
  1405. goto error;
  1406. }
  1407. name = (char *) dentry->d_name.name;
  1408. err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
  1409. if (err < 0)
  1410. goto error;
  1411. /* instantiate inode and assign the unopened fid to the dentry */
  1412. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  1413. fid = p9_client_walk(dfid, 1, &name, 1);
  1414. if (IS_ERR(fid)) {
  1415. err = PTR_ERR(fid);
  1416. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
  1417. err);
  1418. fid = NULL;
  1419. goto error;
  1420. }
  1421. inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
  1422. if (IS_ERR(inode)) {
  1423. err = PTR_ERR(inode);
  1424. P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
  1425. err);
  1426. goto error;
  1427. }
  1428. dentry->d_op = &v9fs_cached_dentry_operations;
  1429. d_instantiate(dentry, inode);
  1430. err = v9fs_fid_add(dentry, fid);
  1431. if (err < 0)
  1432. goto error;
  1433. fid = NULL;
  1434. } else {
  1435. /*
  1436. * Not in cached mode. No need to populate inode with stat.
  1437. * socket syscall returns a fd, so we need instantiate
  1438. */
  1439. inode = v9fs_get_inode(dir->i_sb, mode);
  1440. if (IS_ERR(inode)) {
  1441. err = PTR_ERR(inode);
  1442. goto error;
  1443. }
  1444. dentry->d_op = &v9fs_dentry_operations;
  1445. d_instantiate(dentry, inode);
  1446. }
  1447. error:
  1448. if (fid)
  1449. p9_client_clunk(fid);
  1450. return err;
  1451. }
  1452. static const struct inode_operations v9fs_dir_inode_operations_dotu = {
  1453. .create = v9fs_vfs_create,
  1454. .lookup = v9fs_vfs_lookup,
  1455. .symlink = v9fs_vfs_symlink,
  1456. .link = v9fs_vfs_link,
  1457. .unlink = v9fs_vfs_unlink,
  1458. .mkdir = v9fs_vfs_mkdir,
  1459. .rmdir = v9fs_vfs_rmdir,
  1460. .mknod = v9fs_vfs_mknod_dotl,
  1461. .rename = v9fs_vfs_rename,
  1462. .getattr = v9fs_vfs_getattr,
  1463. .setattr = v9fs_vfs_setattr,
  1464. };
  1465. static const struct inode_operations v9fs_dir_inode_operations_dotl = {
  1466. .create = v9fs_vfs_create,
  1467. .lookup = v9fs_vfs_lookup,
  1468. .link = v9fs_vfs_link_dotl,
  1469. .symlink = v9fs_vfs_symlink_dotl,
  1470. .unlink = v9fs_vfs_unlink,
  1471. .mkdir = v9fs_vfs_mkdir,
  1472. .rmdir = v9fs_vfs_rmdir,
  1473. .mknod = v9fs_vfs_mknod_dotl,
  1474. .rename = v9fs_vfs_rename,
  1475. .getattr = v9fs_vfs_getattr_dotl,
  1476. .setattr = v9fs_vfs_setattr_dotl,
  1477. };
  1478. static const struct inode_operations v9fs_dir_inode_operations = {
  1479. .create = v9fs_vfs_create,
  1480. .lookup = v9fs_vfs_lookup,
  1481. .unlink = v9fs_vfs_unlink,
  1482. .mkdir = v9fs_vfs_mkdir,
  1483. .rmdir = v9fs_vfs_rmdir,
  1484. .mknod = v9fs_vfs_mknod,
  1485. .rename = v9fs_vfs_rename,
  1486. .getattr = v9fs_vfs_getattr,
  1487. .setattr = v9fs_vfs_setattr,
  1488. };
  1489. static const struct inode_operations v9fs_file_inode_operations = {
  1490. .getattr = v9fs_vfs_getattr,
  1491. .setattr = v9fs_vfs_setattr,
  1492. };
  1493. static const struct inode_operations v9fs_file_inode_operations_dotl = {
  1494. .getattr = v9fs_vfs_getattr_dotl,
  1495. .setattr = v9fs_vfs_setattr_dotl,
  1496. };
  1497. static const struct inode_operations v9fs_symlink_inode_operations = {
  1498. .readlink = generic_readlink,
  1499. .follow_link = v9fs_vfs_follow_link,
  1500. .put_link = v9fs_vfs_put_link,
  1501. .getattr = v9fs_vfs_getattr,
  1502. .setattr = v9fs_vfs_setattr,
  1503. };
  1504. static const struct inode_operations v9fs_symlink_inode_operations_dotl = {
  1505. .readlink = generic_readlink,
  1506. .follow_link = v9fs_vfs_follow_link,
  1507. .put_link = v9fs_vfs_put_link,
  1508. .getattr = v9fs_vfs_getattr_dotl,
  1509. .setattr = v9fs_vfs_setattr_dotl,
  1510. };