vfs.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  1. #define MSNFS /* HACK HACK */
  2. /*
  3. * linux/fs/nfsd/vfs.c
  4. *
  5. * File operations used by nfsd. Some of these have been ripped from
  6. * other parts of the kernel because they weren't exported, others
  7. * are partial duplicates with added or changed functionality.
  8. *
  9. * Note that several functions dget() the dentry upon which they want
  10. * to act, most notably those that create directory entries. Response
  11. * dentry's are dput()'d if necessary in the release callback.
  12. * So if you notice code paths that apparently fail to dput() the
  13. * dentry, don't worry--they have been taken care of.
  14. *
  15. * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
  16. * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
  17. */
  18. #include <linux/string.h>
  19. #include <linux/time.h>
  20. #include <linux/errno.h>
  21. #include <linux/fs.h>
  22. #include <linux/file.h>
  23. #include <linux/mount.h>
  24. #include <linux/major.h>
  25. #include <linux/splice.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/stat.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/net.h>
  30. #include <linux/unistd.h>
  31. #include <linux/slab.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/in.h>
  34. #include <linux/module.h>
  35. #include <linux/namei.h>
  36. #include <linux/vfs.h>
  37. #include <linux/delay.h>
  38. #include <linux/sunrpc/svc.h>
  39. #include <linux/nfsd/nfsd.h>
  40. #ifdef CONFIG_NFSD_V3
  41. #include <linux/nfs3.h>
  42. #include <linux/nfsd/xdr3.h>
  43. #endif /* CONFIG_NFSD_V3 */
  44. #include <linux/nfsd/nfsfh.h>
  45. #include <linux/quotaops.h>
  46. #include <linux/fsnotify.h>
  47. #include <linux/posix_acl.h>
  48. #include <linux/posix_acl_xattr.h>
  49. #include <linux/xattr.h>
  50. #ifdef CONFIG_NFSD_V4
  51. #include <linux/nfs4.h>
  52. #include <linux/nfs4_acl.h>
  53. #include <linux/nfsd_idmap.h>
  54. #include <linux/security.h>
  55. #endif /* CONFIG_NFSD_V4 */
  56. #include <linux/jhash.h>
  57. #include <asm/uaccess.h>
  58. #define NFSDDBG_FACILITY NFSDDBG_FILEOP
  59. /*
  60. * This is a cache of readahead params that help us choose the proper
  61. * readahead strategy. Initially, we set all readahead parameters to 0
  62. * and let the VFS handle things.
  63. * If you increase the number of cached files very much, you'll need to
  64. * add a hash table here.
  65. */
  66. struct raparms {
  67. struct raparms *p_next;
  68. unsigned int p_count;
  69. ino_t p_ino;
  70. dev_t p_dev;
  71. int p_set;
  72. struct file_ra_state p_ra;
  73. unsigned int p_hindex;
  74. };
  75. struct raparm_hbucket {
  76. struct raparms *pb_head;
  77. spinlock_t pb_lock;
  78. } ____cacheline_aligned_in_smp;
  79. static struct raparms * raparml;
  80. #define RAPARM_HASH_BITS 4
  81. #define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
  82. #define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
  83. static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
  84. /*
  85. * Called from nfsd_lookup and encode_dirent. Check if we have crossed
  86. * a mount point.
  87. * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
  88. * or nfs_ok having possibly changed *dpp and *expp
  89. */
  90. int
  91. nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
  92. struct svc_export **expp)
  93. {
  94. struct svc_export *exp = *expp, *exp2 = NULL;
  95. struct dentry *dentry = *dpp;
  96. struct vfsmount *mnt = mntget(exp->ex_mnt);
  97. struct dentry *mounts = dget(dentry);
  98. int err = 0;
  99. while (follow_down(&mnt,&mounts)&&d_mountpoint(mounts));
  100. exp2 = rqst_exp_get_by_name(rqstp, mnt, mounts);
  101. if (IS_ERR(exp2)) {
  102. if (PTR_ERR(exp2) != -ENOENT)
  103. err = PTR_ERR(exp2);
  104. dput(mounts);
  105. mntput(mnt);
  106. goto out;
  107. }
  108. if ((exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
  109. /* successfully crossed mount point */
  110. exp_put(exp);
  111. *expp = exp2;
  112. dput(dentry);
  113. *dpp = mounts;
  114. } else {
  115. exp_put(exp2);
  116. dput(mounts);
  117. }
  118. mntput(mnt);
  119. out:
  120. return err;
  121. }
  122. __be32
  123. nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
  124. const char *name, unsigned int len,
  125. struct svc_export **exp_ret, struct dentry **dentry_ret)
  126. {
  127. struct svc_export *exp;
  128. struct dentry *dparent;
  129. struct dentry *dentry;
  130. __be32 err;
  131. int host_err;
  132. dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
  133. /* Obtain dentry and export. */
  134. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_EXEC);
  135. if (err)
  136. return err;
  137. dparent = fhp->fh_dentry;
  138. exp = fhp->fh_export;
  139. exp_get(exp);
  140. /* Lookup the name, but don't follow links */
  141. if (isdotent(name, len)) {
  142. if (len==1)
  143. dentry = dget(dparent);
  144. else if (dparent != exp->ex_dentry) {
  145. dentry = dget_parent(dparent);
  146. } else if (!EX_NOHIDE(exp))
  147. dentry = dget(dparent); /* .. == . just like at / */
  148. else {
  149. /* checking mountpoint crossing is very different when stepping up */
  150. struct svc_export *exp2 = NULL;
  151. struct dentry *dp;
  152. struct vfsmount *mnt = mntget(exp->ex_mnt);
  153. dentry = dget(dparent);
  154. while(dentry == mnt->mnt_root && follow_up(&mnt, &dentry))
  155. ;
  156. dp = dget_parent(dentry);
  157. dput(dentry);
  158. dentry = dp;
  159. exp2 = rqst_exp_parent(rqstp, mnt, dentry);
  160. if (PTR_ERR(exp2) == -ENOENT) {
  161. dput(dentry);
  162. dentry = dget(dparent);
  163. } else if (IS_ERR(exp2)) {
  164. host_err = PTR_ERR(exp2);
  165. dput(dentry);
  166. mntput(mnt);
  167. goto out_nfserr;
  168. } else {
  169. exp_put(exp);
  170. exp = exp2;
  171. }
  172. mntput(mnt);
  173. }
  174. } else {
  175. fh_lock(fhp);
  176. dentry = lookup_one_len(name, dparent, len);
  177. host_err = PTR_ERR(dentry);
  178. if (IS_ERR(dentry))
  179. goto out_nfserr;
  180. /*
  181. * check if we have crossed a mount point ...
  182. */
  183. if (d_mountpoint(dentry)) {
  184. if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
  185. dput(dentry);
  186. goto out_nfserr;
  187. }
  188. }
  189. }
  190. *dentry_ret = dentry;
  191. *exp_ret = exp;
  192. return 0;
  193. out_nfserr:
  194. exp_put(exp);
  195. return nfserrno(host_err);
  196. }
  197. /*
  198. * Look up one component of a pathname.
  199. * N.B. After this call _both_ fhp and resfh need an fh_put
  200. *
  201. * If the lookup would cross a mountpoint, and the mounted filesystem
  202. * is exported to the client with NFSEXP_NOHIDE, then the lookup is
  203. * accepted as it stands and the mounted directory is
  204. * returned. Otherwise the covered directory is returned.
  205. * NOTE: this mountpoint crossing is not supported properly by all
  206. * clients and is explicitly disallowed for NFSv3
  207. * NeilBrown <neilb@cse.unsw.edu.au>
  208. */
  209. __be32
  210. nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
  211. unsigned int len, struct svc_fh *resfh)
  212. {
  213. struct svc_export *exp;
  214. struct dentry *dentry;
  215. __be32 err;
  216. err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
  217. if (err)
  218. return err;
  219. err = check_nfsd_access(exp, rqstp);
  220. if (err)
  221. goto out;
  222. /*
  223. * Note: we compose the file handle now, but as the
  224. * dentry may be negative, it may need to be updated.
  225. */
  226. err = fh_compose(resfh, exp, dentry, fhp);
  227. if (!err && !dentry->d_inode)
  228. err = nfserr_noent;
  229. out:
  230. dput(dentry);
  231. exp_put(exp);
  232. return err;
  233. }
  234. /*
  235. * Set various file attributes.
  236. * N.B. After this call fhp needs an fh_put
  237. */
  238. __be32
  239. nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
  240. int check_guard, time_t guardtime)
  241. {
  242. struct dentry *dentry;
  243. struct inode *inode;
  244. int accmode = MAY_SATTR;
  245. int ftype = 0;
  246. int imode;
  247. __be32 err;
  248. int host_err;
  249. int size_change = 0;
  250. if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
  251. accmode |= MAY_WRITE|MAY_OWNER_OVERRIDE;
  252. if (iap->ia_valid & ATTR_SIZE)
  253. ftype = S_IFREG;
  254. /* Get inode */
  255. err = fh_verify(rqstp, fhp, ftype, accmode);
  256. if (err)
  257. goto out;
  258. dentry = fhp->fh_dentry;
  259. inode = dentry->d_inode;
  260. /* Ignore any mode updates on symlinks */
  261. if (S_ISLNK(inode->i_mode))
  262. iap->ia_valid &= ~ATTR_MODE;
  263. if (!iap->ia_valid)
  264. goto out;
  265. /*
  266. * NFSv2 does not differentiate between "set-[ac]time-to-now"
  267. * which only requires access, and "set-[ac]time-to-X" which
  268. * requires ownership.
  269. * So if it looks like it might be "set both to the same time which
  270. * is close to now", and if inode_change_ok fails, then we
  271. * convert to "set to now" instead of "set to explicit time"
  272. *
  273. * We only call inode_change_ok as the last test as technically
  274. * it is not an interface that we should be using. It is only
  275. * valid if the filesystem does not define it's own i_op->setattr.
  276. */
  277. #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
  278. #define MAX_TOUCH_TIME_ERROR (30*60)
  279. if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
  280. iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
  281. /*
  282. * Looks probable.
  283. *
  284. * Now just make sure time is in the right ballpark.
  285. * Solaris, at least, doesn't seem to care what the time
  286. * request is. We require it be within 30 minutes of now.
  287. */
  288. time_t delta = iap->ia_atime.tv_sec - get_seconds();
  289. if (delta < 0)
  290. delta = -delta;
  291. if (delta < MAX_TOUCH_TIME_ERROR &&
  292. inode_change_ok(inode, iap) != 0) {
  293. /*
  294. * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
  295. * This will cause notify_change to set these times
  296. * to "now"
  297. */
  298. iap->ia_valid &= ~BOTH_TIME_SET;
  299. }
  300. }
  301. /*
  302. * The size case is special.
  303. * It changes the file as well as the attributes.
  304. */
  305. if (iap->ia_valid & ATTR_SIZE) {
  306. if (iap->ia_size < inode->i_size) {
  307. err = nfsd_permission(rqstp, fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
  308. if (err)
  309. goto out;
  310. }
  311. /*
  312. * If we are changing the size of the file, then
  313. * we need to break all leases.
  314. */
  315. host_err = break_lease(inode, FMODE_WRITE | O_NONBLOCK);
  316. if (host_err == -EWOULDBLOCK)
  317. host_err = -ETIMEDOUT;
  318. if (host_err) /* ENOMEM or EWOULDBLOCK */
  319. goto out_nfserr;
  320. host_err = get_write_access(inode);
  321. if (host_err)
  322. goto out_nfserr;
  323. size_change = 1;
  324. host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
  325. if (host_err) {
  326. put_write_access(inode);
  327. goto out_nfserr;
  328. }
  329. DQUOT_INIT(inode);
  330. }
  331. imode = inode->i_mode;
  332. if (iap->ia_valid & ATTR_MODE) {
  333. iap->ia_mode &= S_IALLUGO;
  334. imode = iap->ia_mode |= (imode & ~S_IALLUGO);
  335. /* if changing uid/gid revoke setuid/setgid in mode */
  336. if ((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid) {
  337. iap->ia_valid |= ATTR_KILL_PRIV;
  338. iap->ia_mode &= ~S_ISUID;
  339. }
  340. if ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)
  341. iap->ia_mode &= ~S_ISGID;
  342. } else {
  343. /*
  344. * Revoke setuid/setgid bit on chown/chgrp
  345. */
  346. if ((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid)
  347. iap->ia_valid |= ATTR_KILL_SUID | ATTR_KILL_PRIV;
  348. if ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)
  349. iap->ia_valid |= ATTR_KILL_SGID;
  350. }
  351. /* Change the attributes. */
  352. iap->ia_valid |= ATTR_CTIME;
  353. err = nfserr_notsync;
  354. if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
  355. fh_lock(fhp);
  356. host_err = notify_change(dentry, iap);
  357. err = nfserrno(host_err);
  358. fh_unlock(fhp);
  359. }
  360. if (size_change)
  361. put_write_access(inode);
  362. if (!err)
  363. if (EX_ISSYNC(fhp->fh_export))
  364. write_inode_now(inode, 1);
  365. out:
  366. return err;
  367. out_nfserr:
  368. err = nfserrno(host_err);
  369. goto out;
  370. }
  371. #if defined(CONFIG_NFSD_V2_ACL) || \
  372. defined(CONFIG_NFSD_V3_ACL) || \
  373. defined(CONFIG_NFSD_V4)
  374. static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
  375. {
  376. ssize_t buflen;
  377. buflen = vfs_getxattr(dentry, key, NULL, 0);
  378. if (buflen <= 0)
  379. return buflen;
  380. *buf = kmalloc(buflen, GFP_KERNEL);
  381. if (!*buf)
  382. return -ENOMEM;
  383. return vfs_getxattr(dentry, key, *buf, buflen);
  384. }
  385. #endif
  386. #if defined(CONFIG_NFSD_V4)
  387. static int
  388. set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
  389. {
  390. int len;
  391. size_t buflen;
  392. char *buf = NULL;
  393. int error = 0;
  394. buflen = posix_acl_xattr_size(pacl->a_count);
  395. buf = kmalloc(buflen, GFP_KERNEL);
  396. error = -ENOMEM;
  397. if (buf == NULL)
  398. goto out;
  399. len = posix_acl_to_xattr(pacl, buf, buflen);
  400. if (len < 0) {
  401. error = len;
  402. goto out;
  403. }
  404. error = vfs_setxattr(dentry, key, buf, len, 0);
  405. out:
  406. kfree(buf);
  407. return error;
  408. }
  409. __be32
  410. nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  411. struct nfs4_acl *acl)
  412. {
  413. __be32 error;
  414. int host_error;
  415. struct dentry *dentry;
  416. struct inode *inode;
  417. struct posix_acl *pacl = NULL, *dpacl = NULL;
  418. unsigned int flags = 0;
  419. /* Get inode */
  420. error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, MAY_SATTR);
  421. if (error)
  422. return error;
  423. dentry = fhp->fh_dentry;
  424. inode = dentry->d_inode;
  425. if (S_ISDIR(inode->i_mode))
  426. flags = NFS4_ACL_DIR;
  427. host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
  428. if (host_error == -EINVAL) {
  429. return nfserr_attrnotsupp;
  430. } else if (host_error < 0)
  431. goto out_nfserr;
  432. host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
  433. if (host_error < 0)
  434. goto out_release;
  435. if (S_ISDIR(inode->i_mode))
  436. host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
  437. out_release:
  438. posix_acl_release(pacl);
  439. posix_acl_release(dpacl);
  440. out_nfserr:
  441. if (host_error == -EOPNOTSUPP)
  442. return nfserr_attrnotsupp;
  443. else
  444. return nfserrno(host_error);
  445. }
  446. static struct posix_acl *
  447. _get_posix_acl(struct dentry *dentry, char *key)
  448. {
  449. void *buf = NULL;
  450. struct posix_acl *pacl = NULL;
  451. int buflen;
  452. buflen = nfsd_getxattr(dentry, key, &buf);
  453. if (!buflen)
  454. buflen = -ENODATA;
  455. if (buflen <= 0)
  456. return ERR_PTR(buflen);
  457. pacl = posix_acl_from_xattr(buf, buflen);
  458. kfree(buf);
  459. return pacl;
  460. }
  461. int
  462. nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
  463. {
  464. struct inode *inode = dentry->d_inode;
  465. int error = 0;
  466. struct posix_acl *pacl = NULL, *dpacl = NULL;
  467. unsigned int flags = 0;
  468. pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
  469. if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
  470. pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  471. if (IS_ERR(pacl)) {
  472. error = PTR_ERR(pacl);
  473. pacl = NULL;
  474. goto out;
  475. }
  476. if (S_ISDIR(inode->i_mode)) {
  477. dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
  478. if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
  479. dpacl = NULL;
  480. else if (IS_ERR(dpacl)) {
  481. error = PTR_ERR(dpacl);
  482. dpacl = NULL;
  483. goto out;
  484. }
  485. flags = NFS4_ACL_DIR;
  486. }
  487. *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
  488. if (IS_ERR(*acl)) {
  489. error = PTR_ERR(*acl);
  490. *acl = NULL;
  491. }
  492. out:
  493. posix_acl_release(pacl);
  494. posix_acl_release(dpacl);
  495. return error;
  496. }
  497. #endif /* defined(CONFIG_NFS_V4) */
  498. #ifdef CONFIG_NFSD_V3
  499. /*
  500. * Check server access rights to a file system object
  501. */
  502. struct accessmap {
  503. u32 access;
  504. int how;
  505. };
  506. static struct accessmap nfs3_regaccess[] = {
  507. { NFS3_ACCESS_READ, MAY_READ },
  508. { NFS3_ACCESS_EXECUTE, MAY_EXEC },
  509. { NFS3_ACCESS_MODIFY, MAY_WRITE|MAY_TRUNC },
  510. { NFS3_ACCESS_EXTEND, MAY_WRITE },
  511. { 0, 0 }
  512. };
  513. static struct accessmap nfs3_diraccess[] = {
  514. { NFS3_ACCESS_READ, MAY_READ },
  515. { NFS3_ACCESS_LOOKUP, MAY_EXEC },
  516. { NFS3_ACCESS_MODIFY, MAY_EXEC|MAY_WRITE|MAY_TRUNC },
  517. { NFS3_ACCESS_EXTEND, MAY_EXEC|MAY_WRITE },
  518. { NFS3_ACCESS_DELETE, MAY_REMOVE },
  519. { 0, 0 }
  520. };
  521. static struct accessmap nfs3_anyaccess[] = {
  522. /* Some clients - Solaris 2.6 at least, make an access call
  523. * to the server to check for access for things like /dev/null
  524. * (which really, the server doesn't care about). So
  525. * We provide simple access checking for them, looking
  526. * mainly at mode bits, and we make sure to ignore read-only
  527. * filesystem checks
  528. */
  529. { NFS3_ACCESS_READ, MAY_READ },
  530. { NFS3_ACCESS_EXECUTE, MAY_EXEC },
  531. { NFS3_ACCESS_MODIFY, MAY_WRITE|MAY_LOCAL_ACCESS },
  532. { NFS3_ACCESS_EXTEND, MAY_WRITE|MAY_LOCAL_ACCESS },
  533. { 0, 0 }
  534. };
  535. __be32
  536. nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
  537. {
  538. struct accessmap *map;
  539. struct svc_export *export;
  540. struct dentry *dentry;
  541. u32 query, result = 0, sresult = 0;
  542. __be32 error;
  543. error = fh_verify(rqstp, fhp, 0, MAY_NOP);
  544. if (error)
  545. goto out;
  546. export = fhp->fh_export;
  547. dentry = fhp->fh_dentry;
  548. if (S_ISREG(dentry->d_inode->i_mode))
  549. map = nfs3_regaccess;
  550. else if (S_ISDIR(dentry->d_inode->i_mode))
  551. map = nfs3_diraccess;
  552. else
  553. map = nfs3_anyaccess;
  554. query = *access;
  555. for (; map->access; map++) {
  556. if (map->access & query) {
  557. __be32 err2;
  558. sresult |= map->access;
  559. err2 = nfsd_permission(rqstp, export, dentry, map->how);
  560. switch (err2) {
  561. case nfs_ok:
  562. result |= map->access;
  563. break;
  564. /* the following error codes just mean the access was not allowed,
  565. * rather than an error occurred */
  566. case nfserr_rofs:
  567. case nfserr_acces:
  568. case nfserr_perm:
  569. /* simply don't "or" in the access bit. */
  570. break;
  571. default:
  572. error = err2;
  573. goto out;
  574. }
  575. }
  576. }
  577. *access = result;
  578. if (supported)
  579. *supported = sresult;
  580. out:
  581. return error;
  582. }
  583. #endif /* CONFIG_NFSD_V3 */
  584. /*
  585. * Open an existing file or directory.
  586. * The access argument indicates the type of open (read/write/lock)
  587. * N.B. After this call fhp needs an fh_put
  588. */
  589. __be32
  590. nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  591. int access, struct file **filp)
  592. {
  593. struct dentry *dentry;
  594. struct inode *inode;
  595. int flags = O_RDONLY|O_LARGEFILE;
  596. __be32 err;
  597. int host_err;
  598. /*
  599. * If we get here, then the client has already done an "open",
  600. * and (hopefully) checked permission - so allow OWNER_OVERRIDE
  601. * in case a chmod has now revoked permission.
  602. */
  603. err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE);
  604. if (err)
  605. goto out;
  606. dentry = fhp->fh_dentry;
  607. inode = dentry->d_inode;
  608. /* Disallow write access to files with the append-only bit set
  609. * or any access when mandatory locking enabled
  610. */
  611. err = nfserr_perm;
  612. if (IS_APPEND(inode) && (access & MAY_WRITE))
  613. goto out;
  614. /*
  615. * We must ignore files (but only files) which might have mandatory
  616. * locks on them because there is no way to know if the accesser has
  617. * the lock.
  618. */
  619. if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
  620. goto out;
  621. if (!inode->i_fop)
  622. goto out;
  623. /*
  624. * Check to see if there are any leases on this file.
  625. * This may block while leases are broken.
  626. */
  627. host_err = break_lease(inode, O_NONBLOCK | ((access & MAY_WRITE) ? FMODE_WRITE : 0));
  628. if (host_err == -EWOULDBLOCK)
  629. host_err = -ETIMEDOUT;
  630. if (host_err) /* NOMEM or WOULDBLOCK */
  631. goto out_nfserr;
  632. if (access & MAY_WRITE) {
  633. if (access & MAY_READ)
  634. flags = O_RDWR|O_LARGEFILE;
  635. else
  636. flags = O_WRONLY|O_LARGEFILE;
  637. DQUOT_INIT(inode);
  638. }
  639. *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_mnt), flags);
  640. if (IS_ERR(*filp))
  641. host_err = PTR_ERR(*filp);
  642. out_nfserr:
  643. err = nfserrno(host_err);
  644. out:
  645. return err;
  646. }
  647. /*
  648. * Close a file.
  649. */
  650. void
  651. nfsd_close(struct file *filp)
  652. {
  653. fput(filp);
  654. }
  655. /*
  656. * Sync a file
  657. * As this calls fsync (not fdatasync) there is no need for a write_inode
  658. * after it.
  659. */
  660. static inline int nfsd_dosync(struct file *filp, struct dentry *dp,
  661. const struct file_operations *fop)
  662. {
  663. struct inode *inode = dp->d_inode;
  664. int (*fsync) (struct file *, struct dentry *, int);
  665. int err;
  666. err = filemap_fdatawrite(inode->i_mapping);
  667. if (err == 0 && fop && (fsync = fop->fsync))
  668. err = fsync(filp, dp, 0);
  669. if (err == 0)
  670. err = filemap_fdatawait(inode->i_mapping);
  671. return err;
  672. }
  673. static int
  674. nfsd_sync(struct file *filp)
  675. {
  676. int err;
  677. struct inode *inode = filp->f_path.dentry->d_inode;
  678. dprintk("nfsd: sync file %s\n", filp->f_path.dentry->d_name.name);
  679. mutex_lock(&inode->i_mutex);
  680. err=nfsd_dosync(filp, filp->f_path.dentry, filp->f_op);
  681. mutex_unlock(&inode->i_mutex);
  682. return err;
  683. }
  684. int
  685. nfsd_sync_dir(struct dentry *dp)
  686. {
  687. return nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
  688. }
  689. /*
  690. * Obtain the readahead parameters for the file
  691. * specified by (dev, ino).
  692. */
  693. static inline struct raparms *
  694. nfsd_get_raparms(dev_t dev, ino_t ino)
  695. {
  696. struct raparms *ra, **rap, **frap = NULL;
  697. int depth = 0;
  698. unsigned int hash;
  699. struct raparm_hbucket *rab;
  700. hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
  701. rab = &raparm_hash[hash];
  702. spin_lock(&rab->pb_lock);
  703. for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
  704. if (ra->p_ino == ino && ra->p_dev == dev)
  705. goto found;
  706. depth++;
  707. if (ra->p_count == 0)
  708. frap = rap;
  709. }
  710. depth = nfsdstats.ra_size*11/10;
  711. if (!frap) {
  712. spin_unlock(&rab->pb_lock);
  713. return NULL;
  714. }
  715. rap = frap;
  716. ra = *frap;
  717. ra->p_dev = dev;
  718. ra->p_ino = ino;
  719. ra->p_set = 0;
  720. ra->p_hindex = hash;
  721. found:
  722. if (rap != &rab->pb_head) {
  723. *rap = ra->p_next;
  724. ra->p_next = rab->pb_head;
  725. rab->pb_head = ra;
  726. }
  727. ra->p_count++;
  728. nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
  729. spin_unlock(&rab->pb_lock);
  730. return ra;
  731. }
  732. /*
  733. * Grab and keep cached pages associated with a file in the svc_rqst
  734. * so that they can be passed to the network sendmsg/sendpage routines
  735. * directly. They will be released after the sending has completed.
  736. */
  737. static int
  738. nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  739. struct splice_desc *sd)
  740. {
  741. struct svc_rqst *rqstp = sd->u.data;
  742. struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
  743. struct page *page = buf->page;
  744. size_t size;
  745. int ret;
  746. ret = buf->ops->confirm(pipe, buf);
  747. if (unlikely(ret))
  748. return ret;
  749. size = sd->len;
  750. if (rqstp->rq_res.page_len == 0) {
  751. get_page(page);
  752. put_page(*pp);
  753. *pp = page;
  754. rqstp->rq_resused++;
  755. rqstp->rq_res.page_base = buf->offset;
  756. rqstp->rq_res.page_len = size;
  757. } else if (page != pp[-1]) {
  758. get_page(page);
  759. if (*pp)
  760. put_page(*pp);
  761. *pp = page;
  762. rqstp->rq_resused++;
  763. rqstp->rq_res.page_len += size;
  764. } else
  765. rqstp->rq_res.page_len += size;
  766. return size;
  767. }
  768. static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
  769. struct splice_desc *sd)
  770. {
  771. return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
  772. }
  773. static inline int svc_msnfs(struct svc_fh *ffhp)
  774. {
  775. #ifdef MSNFS
  776. return (ffhp->fh_export->ex_flags & NFSEXP_MSNFS);
  777. #else
  778. return 0;
  779. #endif
  780. }
  781. static __be32
  782. nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  783. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  784. {
  785. struct inode *inode;
  786. struct raparms *ra;
  787. mm_segment_t oldfs;
  788. __be32 err;
  789. int host_err;
  790. err = nfserr_perm;
  791. inode = file->f_path.dentry->d_inode;
  792. if (svc_msnfs(fhp) && !lock_may_read(inode, offset, *count))
  793. goto out;
  794. /* Get readahead parameters */
  795. ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
  796. if (ra && ra->p_set)
  797. file->f_ra = ra->p_ra;
  798. if (file->f_op->splice_read && rqstp->rq_splice_ok) {
  799. struct splice_desc sd = {
  800. .len = 0,
  801. .total_len = *count,
  802. .pos = offset,
  803. .u.data = rqstp,
  804. };
  805. rqstp->rq_resused = 1;
  806. host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
  807. } else {
  808. oldfs = get_fs();
  809. set_fs(KERNEL_DS);
  810. host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
  811. set_fs(oldfs);
  812. }
  813. /* Write back readahead params */
  814. if (ra) {
  815. struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
  816. spin_lock(&rab->pb_lock);
  817. ra->p_ra = file->f_ra;
  818. ra->p_set = 1;
  819. ra->p_count--;
  820. spin_unlock(&rab->pb_lock);
  821. }
  822. if (host_err >= 0) {
  823. nfsdstats.io_read += host_err;
  824. *count = host_err;
  825. err = 0;
  826. fsnotify_access(file->f_path.dentry);
  827. } else
  828. err = nfserrno(host_err);
  829. out:
  830. return err;
  831. }
  832. static void kill_suid(struct dentry *dentry)
  833. {
  834. struct iattr ia;
  835. ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  836. mutex_lock(&dentry->d_inode->i_mutex);
  837. notify_change(dentry, &ia);
  838. mutex_unlock(&dentry->d_inode->i_mutex);
  839. }
  840. static __be32
  841. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  842. loff_t offset, struct kvec *vec, int vlen,
  843. unsigned long cnt, int *stablep)
  844. {
  845. struct svc_export *exp;
  846. struct dentry *dentry;
  847. struct inode *inode;
  848. mm_segment_t oldfs;
  849. __be32 err = 0;
  850. int host_err;
  851. int stable = *stablep;
  852. #ifdef MSNFS
  853. err = nfserr_perm;
  854. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  855. (!lock_may_write(file->f_path.dentry->d_inode, offset, cnt)))
  856. goto out;
  857. #endif
  858. dentry = file->f_path.dentry;
  859. inode = dentry->d_inode;
  860. exp = fhp->fh_export;
  861. /*
  862. * Request sync writes if
  863. * - the sync export option has been set, or
  864. * - the client requested O_SYNC behavior (NFSv3 feature).
  865. * - The file system doesn't support fsync().
  866. * When gathered writes have been configured for this volume,
  867. * flushing the data to disk is handled separately below.
  868. */
  869. if (file->f_op->fsync == 0) {/* COMMIT3 cannot work */
  870. stable = 2;
  871. *stablep = 2; /* FILE_SYNC */
  872. }
  873. if (!EX_ISSYNC(exp))
  874. stable = 0;
  875. if (stable && !EX_WGATHER(exp))
  876. file->f_flags |= O_SYNC;
  877. /* Write the data. */
  878. oldfs = get_fs(); set_fs(KERNEL_DS);
  879. host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
  880. set_fs(oldfs);
  881. if (host_err >= 0) {
  882. nfsdstats.io_write += cnt;
  883. fsnotify_modify(file->f_path.dentry);
  884. }
  885. /* clear setuid/setgid flag after write */
  886. if (host_err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID)))
  887. kill_suid(dentry);
  888. if (host_err >= 0 && stable) {
  889. static ino_t last_ino;
  890. static dev_t last_dev;
  891. /*
  892. * Gathered writes: If another process is currently
  893. * writing to the file, there's a high chance
  894. * this is another nfsd (triggered by a bulk write
  895. * from a client's biod). Rather than syncing the
  896. * file with each write request, we sleep for 10 msec.
  897. *
  898. * I don't know if this roughly approximates
  899. * C. Juszak's idea of gathered writes, but it's a
  900. * nice and simple solution (IMHO), and it seems to
  901. * work:-)
  902. */
  903. if (EX_WGATHER(exp)) {
  904. if (atomic_read(&inode->i_writecount) > 1
  905. || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
  906. dprintk("nfsd: write defer %d\n", task_pid_nr(current));
  907. msleep(10);
  908. dprintk("nfsd: write resume %d\n", task_pid_nr(current));
  909. }
  910. if (inode->i_state & I_DIRTY) {
  911. dprintk("nfsd: write sync %d\n", task_pid_nr(current));
  912. host_err=nfsd_sync(file);
  913. }
  914. #if 0
  915. wake_up(&inode->i_wait);
  916. #endif
  917. }
  918. last_ino = inode->i_ino;
  919. last_dev = inode->i_sb->s_dev;
  920. }
  921. dprintk("nfsd: write complete host_err=%d\n", host_err);
  922. if (host_err >= 0)
  923. err = 0;
  924. else
  925. err = nfserrno(host_err);
  926. out:
  927. return err;
  928. }
  929. /*
  930. * Read data from a file. count must contain the requested read count
  931. * on entry. On return, *count contains the number of bytes actually read.
  932. * N.B. After this call fhp needs an fh_put
  933. */
  934. __be32
  935. nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  936. loff_t offset, struct kvec *vec, int vlen,
  937. unsigned long *count)
  938. {
  939. __be32 err;
  940. if (file) {
  941. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  942. MAY_READ|MAY_OWNER_OVERRIDE);
  943. if (err)
  944. goto out;
  945. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  946. } else {
  947. err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file);
  948. if (err)
  949. goto out;
  950. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  951. nfsd_close(file);
  952. }
  953. out:
  954. return err;
  955. }
  956. /*
  957. * Write data to a file.
  958. * The stable flag requests synchronous writes.
  959. * N.B. After this call fhp needs an fh_put
  960. */
  961. __be32
  962. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  963. loff_t offset, struct kvec *vec, int vlen, unsigned long cnt,
  964. int *stablep)
  965. {
  966. __be32 err = 0;
  967. if (file) {
  968. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  969. MAY_WRITE|MAY_OWNER_OVERRIDE);
  970. if (err)
  971. goto out;
  972. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
  973. stablep);
  974. } else {
  975. err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
  976. if (err)
  977. goto out;
  978. if (cnt)
  979. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
  980. cnt, stablep);
  981. nfsd_close(file);
  982. }
  983. out:
  984. return err;
  985. }
  986. #ifdef CONFIG_NFSD_V3
  987. /*
  988. * Commit all pending writes to stable storage.
  989. * Strictly speaking, we could sync just the indicated file region here,
  990. * but there's currently no way we can ask the VFS to do so.
  991. *
  992. * Unfortunately we cannot lock the file to make sure we return full WCC
  993. * data to the client, as locking happens lower down in the filesystem.
  994. */
  995. __be32
  996. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
  997. loff_t offset, unsigned long count)
  998. {
  999. struct file *file;
  1000. __be32 err;
  1001. if ((u64)count > ~(u64)offset)
  1002. return nfserr_inval;
  1003. if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
  1004. return err;
  1005. if (EX_ISSYNC(fhp->fh_export)) {
  1006. if (file->f_op && file->f_op->fsync) {
  1007. err = nfserrno(nfsd_sync(file));
  1008. } else {
  1009. err = nfserr_notsupp;
  1010. }
  1011. }
  1012. nfsd_close(file);
  1013. return err;
  1014. }
  1015. #endif /* CONFIG_NFSD_V3 */
  1016. __be32
  1017. nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
  1018. struct iattr *iap)
  1019. {
  1020. /*
  1021. * Mode has already been set earlier in create:
  1022. */
  1023. iap->ia_valid &= ~ATTR_MODE;
  1024. /*
  1025. * Setting uid/gid works only for root. Irix appears to
  1026. * send along the gid on create when it tries to implement
  1027. * setgid directories via NFS:
  1028. */
  1029. if (current->fsuid != 0)
  1030. iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
  1031. if (iap->ia_valid)
  1032. return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1033. return 0;
  1034. }
  1035. /*
  1036. * Create a file (regular, directory, device, fifo); UNIX sockets
  1037. * not yet implemented.
  1038. * If the response fh has been verified, the parent directory should
  1039. * already be locked. Note that the parent directory is left locked.
  1040. *
  1041. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  1042. */
  1043. __be32
  1044. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1045. char *fname, int flen, struct iattr *iap,
  1046. int type, dev_t rdev, struct svc_fh *resfhp)
  1047. {
  1048. struct dentry *dentry, *dchild = NULL;
  1049. struct inode *dirp;
  1050. __be32 err;
  1051. __be32 err2;
  1052. int host_err;
  1053. err = nfserr_perm;
  1054. if (!flen)
  1055. goto out;
  1056. err = nfserr_exist;
  1057. if (isdotent(fname, flen))
  1058. goto out;
  1059. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1060. if (err)
  1061. goto out;
  1062. dentry = fhp->fh_dentry;
  1063. dirp = dentry->d_inode;
  1064. err = nfserr_notdir;
  1065. if(!dirp->i_op || !dirp->i_op->lookup)
  1066. goto out;
  1067. /*
  1068. * Check whether the response file handle has been verified yet.
  1069. * If it has, the parent directory should already be locked.
  1070. */
  1071. if (!resfhp->fh_dentry) {
  1072. /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
  1073. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1074. dchild = lookup_one_len(fname, dentry, flen);
  1075. host_err = PTR_ERR(dchild);
  1076. if (IS_ERR(dchild))
  1077. goto out_nfserr;
  1078. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1079. if (err)
  1080. goto out;
  1081. } else {
  1082. /* called from nfsd_proc_create */
  1083. dchild = dget(resfhp->fh_dentry);
  1084. if (!fhp->fh_locked) {
  1085. /* not actually possible */
  1086. printk(KERN_ERR
  1087. "nfsd_create: parent %s/%s not locked!\n",
  1088. dentry->d_parent->d_name.name,
  1089. dentry->d_name.name);
  1090. err = nfserr_io;
  1091. goto out;
  1092. }
  1093. }
  1094. /*
  1095. * Make sure the child dentry is still negative ...
  1096. */
  1097. err = nfserr_exist;
  1098. if (dchild->d_inode) {
  1099. dprintk("nfsd_create: dentry %s/%s not negative!\n",
  1100. dentry->d_name.name, dchild->d_name.name);
  1101. goto out;
  1102. }
  1103. if (!(iap->ia_valid & ATTR_MODE))
  1104. iap->ia_mode = 0;
  1105. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1106. /*
  1107. * Get the dir op function pointer.
  1108. */
  1109. err = 0;
  1110. switch (type) {
  1111. case S_IFREG:
  1112. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1113. break;
  1114. case S_IFDIR:
  1115. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1116. break;
  1117. case S_IFCHR:
  1118. case S_IFBLK:
  1119. case S_IFIFO:
  1120. case S_IFSOCK:
  1121. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1122. break;
  1123. default:
  1124. printk("nfsd: bad file type %o in nfsd_create\n", type);
  1125. host_err = -EINVAL;
  1126. }
  1127. if (host_err < 0)
  1128. goto out_nfserr;
  1129. if (EX_ISSYNC(fhp->fh_export)) {
  1130. err = nfserrno(nfsd_sync_dir(dentry));
  1131. write_inode_now(dchild->d_inode, 1);
  1132. }
  1133. err2 = nfsd_create_setattr(rqstp, resfhp, iap);
  1134. if (err2)
  1135. err = err2;
  1136. /*
  1137. * Update the file handle to get the new inode info.
  1138. */
  1139. if (!err)
  1140. err = fh_update(resfhp);
  1141. out:
  1142. if (dchild && !IS_ERR(dchild))
  1143. dput(dchild);
  1144. return err;
  1145. out_nfserr:
  1146. err = nfserrno(host_err);
  1147. goto out;
  1148. }
  1149. #ifdef CONFIG_NFSD_V3
  1150. /*
  1151. * NFSv3 version of nfsd_create
  1152. */
  1153. __be32
  1154. nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1155. char *fname, int flen, struct iattr *iap,
  1156. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1157. int *truncp, int *created)
  1158. {
  1159. struct dentry *dentry, *dchild = NULL;
  1160. struct inode *dirp;
  1161. __be32 err;
  1162. __be32 err2;
  1163. int host_err;
  1164. __u32 v_mtime=0, v_atime=0;
  1165. err = nfserr_perm;
  1166. if (!flen)
  1167. goto out;
  1168. err = nfserr_exist;
  1169. if (isdotent(fname, flen))
  1170. goto out;
  1171. if (!(iap->ia_valid & ATTR_MODE))
  1172. iap->ia_mode = 0;
  1173. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1174. if (err)
  1175. goto out;
  1176. dentry = fhp->fh_dentry;
  1177. dirp = dentry->d_inode;
  1178. /* Get all the sanity checks out of the way before
  1179. * we lock the parent. */
  1180. err = nfserr_notdir;
  1181. if(!dirp->i_op || !dirp->i_op->lookup)
  1182. goto out;
  1183. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1184. /*
  1185. * Compose the response file handle.
  1186. */
  1187. dchild = lookup_one_len(fname, dentry, flen);
  1188. host_err = PTR_ERR(dchild);
  1189. if (IS_ERR(dchild))
  1190. goto out_nfserr;
  1191. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1192. if (err)
  1193. goto out;
  1194. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1195. /* solaris7 gets confused (bugid 4218508) if these have
  1196. * the high bit set, so just clear the high bits. If this is
  1197. * ever changed to use different attrs for storing the
  1198. * verifier, then do_open_lookup() will also need to be fixed
  1199. * accordingly.
  1200. */
  1201. v_mtime = verifier[0]&0x7fffffff;
  1202. v_atime = verifier[1]&0x7fffffff;
  1203. }
  1204. if (dchild->d_inode) {
  1205. err = 0;
  1206. switch (createmode) {
  1207. case NFS3_CREATE_UNCHECKED:
  1208. if (! S_ISREG(dchild->d_inode->i_mode))
  1209. err = nfserr_exist;
  1210. else if (truncp) {
  1211. /* in nfsv4, we need to treat this case a little
  1212. * differently. we don't want to truncate the
  1213. * file now; this would be wrong if the OPEN
  1214. * fails for some other reason. furthermore,
  1215. * if the size is nonzero, we should ignore it
  1216. * according to spec!
  1217. */
  1218. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1219. }
  1220. else {
  1221. iap->ia_valid &= ATTR_SIZE;
  1222. goto set_attr;
  1223. }
  1224. break;
  1225. case NFS3_CREATE_EXCLUSIVE:
  1226. if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
  1227. && dchild->d_inode->i_atime.tv_sec == v_atime
  1228. && dchild->d_inode->i_size == 0 )
  1229. break;
  1230. /* fallthru */
  1231. case NFS3_CREATE_GUARDED:
  1232. err = nfserr_exist;
  1233. }
  1234. goto out;
  1235. }
  1236. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1237. if (host_err < 0)
  1238. goto out_nfserr;
  1239. if (created)
  1240. *created = 1;
  1241. if (EX_ISSYNC(fhp->fh_export)) {
  1242. err = nfserrno(nfsd_sync_dir(dentry));
  1243. /* setattr will sync the child (or not) */
  1244. }
  1245. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1246. /* Cram the verifier into atime/mtime */
  1247. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1248. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1249. /* XXX someone who knows this better please fix it for nsec */
  1250. iap->ia_mtime.tv_sec = v_mtime;
  1251. iap->ia_atime.tv_sec = v_atime;
  1252. iap->ia_mtime.tv_nsec = 0;
  1253. iap->ia_atime.tv_nsec = 0;
  1254. }
  1255. set_attr:
  1256. err2 = nfsd_create_setattr(rqstp, resfhp, iap);
  1257. if (err2)
  1258. err = err2;
  1259. /*
  1260. * Update the filehandle to get the new inode info.
  1261. */
  1262. if (!err)
  1263. err = fh_update(resfhp);
  1264. out:
  1265. fh_unlock(fhp);
  1266. if (dchild && !IS_ERR(dchild))
  1267. dput(dchild);
  1268. return err;
  1269. out_nfserr:
  1270. err = nfserrno(host_err);
  1271. goto out;
  1272. }
  1273. #endif /* CONFIG_NFSD_V3 */
  1274. /*
  1275. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1276. * fits into the buffer. On return, it contains the true length.
  1277. * N.B. After this call fhp needs an fh_put
  1278. */
  1279. __be32
  1280. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1281. {
  1282. struct dentry *dentry;
  1283. struct inode *inode;
  1284. mm_segment_t oldfs;
  1285. __be32 err;
  1286. int host_err;
  1287. err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
  1288. if (err)
  1289. goto out;
  1290. dentry = fhp->fh_dentry;
  1291. inode = dentry->d_inode;
  1292. err = nfserr_inval;
  1293. if (!inode->i_op || !inode->i_op->readlink)
  1294. goto out;
  1295. touch_atime(fhp->fh_export->ex_mnt, dentry);
  1296. /* N.B. Why does this call need a get_fs()??
  1297. * Remove the set_fs and watch the fireworks:-) --okir
  1298. */
  1299. oldfs = get_fs(); set_fs(KERNEL_DS);
  1300. host_err = inode->i_op->readlink(dentry, buf, *lenp);
  1301. set_fs(oldfs);
  1302. if (host_err < 0)
  1303. goto out_nfserr;
  1304. *lenp = host_err;
  1305. err = 0;
  1306. out:
  1307. return err;
  1308. out_nfserr:
  1309. err = nfserrno(host_err);
  1310. goto out;
  1311. }
  1312. /*
  1313. * Create a symlink and look up its inode
  1314. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1315. */
  1316. __be32
  1317. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1318. char *fname, int flen,
  1319. char *path, int plen,
  1320. struct svc_fh *resfhp,
  1321. struct iattr *iap)
  1322. {
  1323. struct dentry *dentry, *dnew;
  1324. __be32 err, cerr;
  1325. int host_err;
  1326. umode_t mode;
  1327. err = nfserr_noent;
  1328. if (!flen || !plen)
  1329. goto out;
  1330. err = nfserr_exist;
  1331. if (isdotent(fname, flen))
  1332. goto out;
  1333. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1334. if (err)
  1335. goto out;
  1336. fh_lock(fhp);
  1337. dentry = fhp->fh_dentry;
  1338. dnew = lookup_one_len(fname, dentry, flen);
  1339. host_err = PTR_ERR(dnew);
  1340. if (IS_ERR(dnew))
  1341. goto out_nfserr;
  1342. mode = S_IALLUGO;
  1343. /* Only the MODE ATTRibute is even vaguely meaningful */
  1344. if (iap && (iap->ia_valid & ATTR_MODE))
  1345. mode = iap->ia_mode & S_IALLUGO;
  1346. if (unlikely(path[plen] != 0)) {
  1347. char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
  1348. if (path_alloced == NULL)
  1349. host_err = -ENOMEM;
  1350. else {
  1351. strncpy(path_alloced, path, plen);
  1352. path_alloced[plen] = 0;
  1353. host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced, mode);
  1354. kfree(path_alloced);
  1355. }
  1356. } else
  1357. host_err = vfs_symlink(dentry->d_inode, dnew, path, mode);
  1358. if (!host_err) {
  1359. if (EX_ISSYNC(fhp->fh_export))
  1360. host_err = nfsd_sync_dir(dentry);
  1361. }
  1362. err = nfserrno(host_err);
  1363. fh_unlock(fhp);
  1364. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1365. dput(dnew);
  1366. if (err==0) err = cerr;
  1367. out:
  1368. return err;
  1369. out_nfserr:
  1370. err = nfserrno(host_err);
  1371. goto out;
  1372. }
  1373. /*
  1374. * Create a hardlink
  1375. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1376. */
  1377. __be32
  1378. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1379. char *name, int len, struct svc_fh *tfhp)
  1380. {
  1381. struct dentry *ddir, *dnew, *dold;
  1382. struct inode *dirp, *dest;
  1383. __be32 err;
  1384. int host_err;
  1385. err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
  1386. if (err)
  1387. goto out;
  1388. err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
  1389. if (err)
  1390. goto out;
  1391. err = nfserr_perm;
  1392. if (!len)
  1393. goto out;
  1394. err = nfserr_exist;
  1395. if (isdotent(name, len))
  1396. goto out;
  1397. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1398. ddir = ffhp->fh_dentry;
  1399. dirp = ddir->d_inode;
  1400. dnew = lookup_one_len(name, ddir, len);
  1401. host_err = PTR_ERR(dnew);
  1402. if (IS_ERR(dnew))
  1403. goto out_nfserr;
  1404. dold = tfhp->fh_dentry;
  1405. dest = dold->d_inode;
  1406. host_err = vfs_link(dold, dirp, dnew);
  1407. if (!host_err) {
  1408. if (EX_ISSYNC(ffhp->fh_export)) {
  1409. err = nfserrno(nfsd_sync_dir(ddir));
  1410. write_inode_now(dest, 1);
  1411. }
  1412. err = 0;
  1413. } else {
  1414. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1415. err = nfserr_acces;
  1416. else
  1417. err = nfserrno(host_err);
  1418. }
  1419. dput(dnew);
  1420. out_unlock:
  1421. fh_unlock(ffhp);
  1422. out:
  1423. return err;
  1424. out_nfserr:
  1425. err = nfserrno(host_err);
  1426. goto out_unlock;
  1427. }
  1428. /*
  1429. * Rename a file
  1430. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1431. */
  1432. __be32
  1433. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1434. struct svc_fh *tfhp, char *tname, int tlen)
  1435. {
  1436. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1437. struct inode *fdir, *tdir;
  1438. __be32 err;
  1439. int host_err;
  1440. err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
  1441. if (err)
  1442. goto out;
  1443. err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
  1444. if (err)
  1445. goto out;
  1446. fdentry = ffhp->fh_dentry;
  1447. fdir = fdentry->d_inode;
  1448. tdentry = tfhp->fh_dentry;
  1449. tdir = tdentry->d_inode;
  1450. err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
  1451. if (ffhp->fh_export != tfhp->fh_export)
  1452. goto out;
  1453. err = nfserr_perm;
  1454. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1455. goto out;
  1456. /* cannot use fh_lock as we need deadlock protective ordering
  1457. * so do it by hand */
  1458. trap = lock_rename(tdentry, fdentry);
  1459. ffhp->fh_locked = tfhp->fh_locked = 1;
  1460. fill_pre_wcc(ffhp);
  1461. fill_pre_wcc(tfhp);
  1462. odentry = lookup_one_len(fname, fdentry, flen);
  1463. host_err = PTR_ERR(odentry);
  1464. if (IS_ERR(odentry))
  1465. goto out_nfserr;
  1466. host_err = -ENOENT;
  1467. if (!odentry->d_inode)
  1468. goto out_dput_old;
  1469. host_err = -EINVAL;
  1470. if (odentry == trap)
  1471. goto out_dput_old;
  1472. ndentry = lookup_one_len(tname, tdentry, tlen);
  1473. host_err = PTR_ERR(ndentry);
  1474. if (IS_ERR(ndentry))
  1475. goto out_dput_old;
  1476. host_err = -ENOTEMPTY;
  1477. if (ndentry == trap)
  1478. goto out_dput_new;
  1479. #ifdef MSNFS
  1480. if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  1481. ((atomic_read(&odentry->d_count) > 1)
  1482. || (atomic_read(&ndentry->d_count) > 1))) {
  1483. host_err = -EPERM;
  1484. } else
  1485. #endif
  1486. host_err = vfs_rename(fdir, odentry, tdir, ndentry);
  1487. if (!host_err && EX_ISSYNC(tfhp->fh_export)) {
  1488. host_err = nfsd_sync_dir(tdentry);
  1489. if (!host_err)
  1490. host_err = nfsd_sync_dir(fdentry);
  1491. }
  1492. out_dput_new:
  1493. dput(ndentry);
  1494. out_dput_old:
  1495. dput(odentry);
  1496. out_nfserr:
  1497. err = nfserrno(host_err);
  1498. /* we cannot reply on fh_unlock on the two filehandles,
  1499. * as that would do the wrong thing if the two directories
  1500. * were the same, so again we do it by hand
  1501. */
  1502. fill_post_wcc(ffhp);
  1503. fill_post_wcc(tfhp);
  1504. unlock_rename(tdentry, fdentry);
  1505. ffhp->fh_locked = tfhp->fh_locked = 0;
  1506. out:
  1507. return err;
  1508. }
  1509. /*
  1510. * Unlink a file or directory
  1511. * N.B. After this call fhp needs an fh_put
  1512. */
  1513. __be32
  1514. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1515. char *fname, int flen)
  1516. {
  1517. struct dentry *dentry, *rdentry;
  1518. struct inode *dirp;
  1519. __be32 err;
  1520. int host_err;
  1521. err = nfserr_acces;
  1522. if (!flen || isdotent(fname, flen))
  1523. goto out;
  1524. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
  1525. if (err)
  1526. goto out;
  1527. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1528. dentry = fhp->fh_dentry;
  1529. dirp = dentry->d_inode;
  1530. rdentry = lookup_one_len(fname, dentry, flen);
  1531. host_err = PTR_ERR(rdentry);
  1532. if (IS_ERR(rdentry))
  1533. goto out_nfserr;
  1534. if (!rdentry->d_inode) {
  1535. dput(rdentry);
  1536. err = nfserr_noent;
  1537. goto out;
  1538. }
  1539. if (!type)
  1540. type = rdentry->d_inode->i_mode & S_IFMT;
  1541. if (type != S_IFDIR) { /* It's UNLINK */
  1542. #ifdef MSNFS
  1543. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  1544. (atomic_read(&rdentry->d_count) > 1)) {
  1545. host_err = -EPERM;
  1546. } else
  1547. #endif
  1548. host_err = vfs_unlink(dirp, rdentry);
  1549. } else { /* It's RMDIR */
  1550. host_err = vfs_rmdir(dirp, rdentry);
  1551. }
  1552. dput(rdentry);
  1553. if (host_err)
  1554. goto out_nfserr;
  1555. if (EX_ISSYNC(fhp->fh_export))
  1556. host_err = nfsd_sync_dir(dentry);
  1557. out_nfserr:
  1558. err = nfserrno(host_err);
  1559. out:
  1560. return err;
  1561. }
  1562. /*
  1563. * Read entries from a directory.
  1564. * The NFSv3/4 verifier we ignore for now.
  1565. */
  1566. __be32
  1567. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1568. struct readdir_cd *cdp, filldir_t func)
  1569. {
  1570. __be32 err;
  1571. int host_err;
  1572. struct file *file;
  1573. loff_t offset = *offsetp;
  1574. err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
  1575. if (err)
  1576. goto out;
  1577. offset = vfs_llseek(file, offset, 0);
  1578. if (offset < 0) {
  1579. err = nfserrno((int)offset);
  1580. goto out_close;
  1581. }
  1582. /*
  1583. * Read the directory entries. This silly loop is necessary because
  1584. * readdir() is not guaranteed to fill up the entire buffer, but
  1585. * may choose to do less.
  1586. */
  1587. do {
  1588. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1589. host_err = vfs_readdir(file, func, cdp);
  1590. } while (host_err >=0 && cdp->err == nfs_ok);
  1591. if (host_err)
  1592. err = nfserrno(host_err);
  1593. else
  1594. err = cdp->err;
  1595. *offsetp = vfs_llseek(file, 0, 1);
  1596. if (err == nfserr_eof || err == nfserr_toosmall)
  1597. err = nfs_ok; /* can still be found in ->err */
  1598. out_close:
  1599. nfsd_close(file);
  1600. out:
  1601. return err;
  1602. }
  1603. /*
  1604. * Get file system stats
  1605. * N.B. After this call fhp needs an fh_put
  1606. */
  1607. __be32
  1608. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat)
  1609. {
  1610. __be32 err = fh_verify(rqstp, fhp, 0, MAY_NOP);
  1611. if (!err && vfs_statfs(fhp->fh_dentry,stat))
  1612. err = nfserr_io;
  1613. return err;
  1614. }
  1615. static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
  1616. {
  1617. return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
  1618. }
  1619. /*
  1620. * Check for a user's access permissions to this inode.
  1621. */
  1622. __be32
  1623. nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
  1624. struct dentry *dentry, int acc)
  1625. {
  1626. struct inode *inode = dentry->d_inode;
  1627. int err;
  1628. if (acc == MAY_NOP)
  1629. return 0;
  1630. #if 0
  1631. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1632. acc,
  1633. (acc & MAY_READ)? " read" : "",
  1634. (acc & MAY_WRITE)? " write" : "",
  1635. (acc & MAY_EXEC)? " exec" : "",
  1636. (acc & MAY_SATTR)? " sattr" : "",
  1637. (acc & MAY_TRUNC)? " trunc" : "",
  1638. (acc & MAY_LOCK)? " lock" : "",
  1639. (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1640. inode->i_mode,
  1641. IS_IMMUTABLE(inode)? " immut" : "",
  1642. IS_APPEND(inode)? " append" : "",
  1643. IS_RDONLY(inode)? " ro" : "");
  1644. dprintk(" owner %d/%d user %d/%d\n",
  1645. inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
  1646. #endif
  1647. /* Normally we reject any write/sattr etc access on a read-only file
  1648. * system. But if it is IRIX doing check on write-access for a
  1649. * device special file, we ignore rofs.
  1650. */
  1651. if (!(acc & MAY_LOCAL_ACCESS))
  1652. if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
  1653. if (exp_rdonly(rqstp, exp) || IS_RDONLY(inode))
  1654. return nfserr_rofs;
  1655. if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1656. return nfserr_perm;
  1657. }
  1658. if ((acc & MAY_TRUNC) && IS_APPEND(inode))
  1659. return nfserr_perm;
  1660. if (acc & MAY_LOCK) {
  1661. /* If we cannot rely on authentication in NLM requests,
  1662. * just allow locks, otherwise require read permission, or
  1663. * ownership
  1664. */
  1665. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1666. return 0;
  1667. else
  1668. acc = MAY_READ | MAY_OWNER_OVERRIDE;
  1669. }
  1670. /*
  1671. * The file owner always gets access permission for accesses that
  1672. * would normally be checked at open time. This is to make
  1673. * file access work even when the client has done a fchmod(fd, 0).
  1674. *
  1675. * However, `cp foo bar' should fail nevertheless when bar is
  1676. * readonly. A sensible way to do this might be to reject all
  1677. * attempts to truncate a read-only file, because a creat() call
  1678. * always implies file truncation.
  1679. * ... but this isn't really fair. A process may reasonably call
  1680. * ftruncate on an open file descriptor on a file with perm 000.
  1681. * We must trust the client to do permission checking - using "ACCESS"
  1682. * with NFSv3.
  1683. */
  1684. if ((acc & MAY_OWNER_OVERRIDE) &&
  1685. inode->i_uid == current->fsuid)
  1686. return 0;
  1687. err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC), NULL);
  1688. /* Allow read access to binaries even when mode 111 */
  1689. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1690. acc == (MAY_READ | MAY_OWNER_OVERRIDE))
  1691. err = permission(inode, MAY_EXEC, NULL);
  1692. return err? nfserrno(err) : 0;
  1693. }
  1694. void
  1695. nfsd_racache_shutdown(void)
  1696. {
  1697. if (!raparml)
  1698. return;
  1699. dprintk("nfsd: freeing readahead buffers.\n");
  1700. kfree(raparml);
  1701. raparml = NULL;
  1702. }
  1703. /*
  1704. * Initialize readahead param cache
  1705. */
  1706. int
  1707. nfsd_racache_init(int cache_size)
  1708. {
  1709. int i;
  1710. int j = 0;
  1711. int nperbucket;
  1712. if (raparml)
  1713. return 0;
  1714. if (cache_size < 2*RAPARM_HASH_SIZE)
  1715. cache_size = 2*RAPARM_HASH_SIZE;
  1716. raparml = kcalloc(cache_size, sizeof(struct raparms), GFP_KERNEL);
  1717. if (!raparml) {
  1718. printk(KERN_WARNING
  1719. "nfsd: Could not allocate memory read-ahead cache.\n");
  1720. return -ENOMEM;
  1721. }
  1722. dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
  1723. for (i = 0 ; i < RAPARM_HASH_SIZE ; i++) {
  1724. raparm_hash[i].pb_head = NULL;
  1725. spin_lock_init(&raparm_hash[i].pb_lock);
  1726. }
  1727. nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
  1728. for (i = 0; i < cache_size - 1; i++) {
  1729. if (i % nperbucket == 0)
  1730. raparm_hash[j++].pb_head = raparml + i;
  1731. if (i % nperbucket < nperbucket-1)
  1732. raparml[i].p_next = raparml + i + 1;
  1733. }
  1734. nfsdstats.ra_size = cache_size;
  1735. return 0;
  1736. }
  1737. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  1738. struct posix_acl *
  1739. nfsd_get_posix_acl(struct svc_fh *fhp, int type)
  1740. {
  1741. struct inode *inode = fhp->fh_dentry->d_inode;
  1742. char *name;
  1743. void *value = NULL;
  1744. ssize_t size;
  1745. struct posix_acl *acl;
  1746. if (!IS_POSIXACL(inode))
  1747. return ERR_PTR(-EOPNOTSUPP);
  1748. switch (type) {
  1749. case ACL_TYPE_ACCESS:
  1750. name = POSIX_ACL_XATTR_ACCESS;
  1751. break;
  1752. case ACL_TYPE_DEFAULT:
  1753. name = POSIX_ACL_XATTR_DEFAULT;
  1754. break;
  1755. default:
  1756. return ERR_PTR(-EOPNOTSUPP);
  1757. }
  1758. size = nfsd_getxattr(fhp->fh_dentry, name, &value);
  1759. if (size < 0)
  1760. return ERR_PTR(size);
  1761. acl = posix_acl_from_xattr(value, size);
  1762. kfree(value);
  1763. return acl;
  1764. }
  1765. int
  1766. nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
  1767. {
  1768. struct inode *inode = fhp->fh_dentry->d_inode;
  1769. char *name;
  1770. void *value = NULL;
  1771. size_t size;
  1772. int error;
  1773. if (!IS_POSIXACL(inode) || !inode->i_op ||
  1774. !inode->i_op->setxattr || !inode->i_op->removexattr)
  1775. return -EOPNOTSUPP;
  1776. switch(type) {
  1777. case ACL_TYPE_ACCESS:
  1778. name = POSIX_ACL_XATTR_ACCESS;
  1779. break;
  1780. case ACL_TYPE_DEFAULT:
  1781. name = POSIX_ACL_XATTR_DEFAULT;
  1782. break;
  1783. default:
  1784. return -EOPNOTSUPP;
  1785. }
  1786. if (acl && acl->a_count) {
  1787. size = posix_acl_xattr_size(acl->a_count);
  1788. value = kmalloc(size, GFP_KERNEL);
  1789. if (!value)
  1790. return -ENOMEM;
  1791. error = posix_acl_to_xattr(acl, value, size);
  1792. if (error < 0)
  1793. goto getout;
  1794. size = error;
  1795. } else
  1796. size = 0;
  1797. if (size)
  1798. error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
  1799. else {
  1800. if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
  1801. error = 0;
  1802. else {
  1803. error = vfs_removexattr(fhp->fh_dentry, name);
  1804. if (error == -ENODATA)
  1805. error = 0;
  1806. }
  1807. }
  1808. getout:
  1809. kfree(value);
  1810. return error;
  1811. }
  1812. #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */