vfs_inode.c 33 KB

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