vfs.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  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, 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. 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. /*
  1017. * Create a file (regular, directory, device, fifo); UNIX sockets
  1018. * not yet implemented.
  1019. * If the response fh has been verified, the parent directory should
  1020. * already be locked. Note that the parent directory is left locked.
  1021. *
  1022. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  1023. */
  1024. __be32
  1025. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1026. char *fname, int flen, struct iattr *iap,
  1027. int type, dev_t rdev, struct svc_fh *resfhp)
  1028. {
  1029. struct dentry *dentry, *dchild = NULL;
  1030. struct inode *dirp;
  1031. __be32 err;
  1032. int host_err;
  1033. err = nfserr_perm;
  1034. if (!flen)
  1035. goto out;
  1036. err = nfserr_exist;
  1037. if (isdotent(fname, flen))
  1038. goto out;
  1039. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1040. if (err)
  1041. goto out;
  1042. dentry = fhp->fh_dentry;
  1043. dirp = dentry->d_inode;
  1044. err = nfserr_notdir;
  1045. if(!dirp->i_op || !dirp->i_op->lookup)
  1046. goto out;
  1047. /*
  1048. * Check whether the response file handle has been verified yet.
  1049. * If it has, the parent directory should already be locked.
  1050. */
  1051. if (!resfhp->fh_dentry) {
  1052. /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
  1053. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1054. dchild = lookup_one_len(fname, dentry, flen);
  1055. host_err = PTR_ERR(dchild);
  1056. if (IS_ERR(dchild))
  1057. goto out_nfserr;
  1058. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1059. if (err)
  1060. goto out;
  1061. } else {
  1062. /* called from nfsd_proc_create */
  1063. dchild = dget(resfhp->fh_dentry);
  1064. if (!fhp->fh_locked) {
  1065. /* not actually possible */
  1066. printk(KERN_ERR
  1067. "nfsd_create: parent %s/%s not locked!\n",
  1068. dentry->d_parent->d_name.name,
  1069. dentry->d_name.name);
  1070. err = nfserr_io;
  1071. goto out;
  1072. }
  1073. }
  1074. /*
  1075. * Make sure the child dentry is still negative ...
  1076. */
  1077. err = nfserr_exist;
  1078. if (dchild->d_inode) {
  1079. dprintk("nfsd_create: dentry %s/%s not negative!\n",
  1080. dentry->d_name.name, dchild->d_name.name);
  1081. goto out;
  1082. }
  1083. if (!(iap->ia_valid & ATTR_MODE))
  1084. iap->ia_mode = 0;
  1085. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1086. /*
  1087. * Get the dir op function pointer.
  1088. */
  1089. err = 0;
  1090. switch (type) {
  1091. case S_IFREG:
  1092. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1093. break;
  1094. case S_IFDIR:
  1095. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1096. break;
  1097. case S_IFCHR:
  1098. case S_IFBLK:
  1099. case S_IFIFO:
  1100. case S_IFSOCK:
  1101. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1102. break;
  1103. default:
  1104. printk("nfsd: bad file type %o in nfsd_create\n", type);
  1105. host_err = -EINVAL;
  1106. }
  1107. if (host_err < 0)
  1108. goto out_nfserr;
  1109. if (EX_ISSYNC(fhp->fh_export)) {
  1110. err = nfserrno(nfsd_sync_dir(dentry));
  1111. write_inode_now(dchild->d_inode, 1);
  1112. }
  1113. /* Set file attributes. Mode has already been set and
  1114. * setting uid/gid works only for root. Irix appears to
  1115. * send along the gid when it tries to implement setgid
  1116. * directories via NFS.
  1117. */
  1118. if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0) {
  1119. __be32 err2 = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1120. if (err2)
  1121. err = err2;
  1122. }
  1123. /*
  1124. * Update the file handle to get the new inode info.
  1125. */
  1126. if (!err)
  1127. err = fh_update(resfhp);
  1128. out:
  1129. if (dchild && !IS_ERR(dchild))
  1130. dput(dchild);
  1131. return err;
  1132. out_nfserr:
  1133. err = nfserrno(host_err);
  1134. goto out;
  1135. }
  1136. #ifdef CONFIG_NFSD_V3
  1137. /*
  1138. * NFSv3 version of nfsd_create
  1139. */
  1140. __be32
  1141. nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1142. char *fname, int flen, struct iattr *iap,
  1143. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1144. int *truncp, int *created)
  1145. {
  1146. struct dentry *dentry, *dchild = NULL;
  1147. struct inode *dirp;
  1148. __be32 err;
  1149. int host_err;
  1150. __u32 v_mtime=0, v_atime=0;
  1151. err = nfserr_perm;
  1152. if (!flen)
  1153. goto out;
  1154. err = nfserr_exist;
  1155. if (isdotent(fname, flen))
  1156. goto out;
  1157. if (!(iap->ia_valid & ATTR_MODE))
  1158. iap->ia_mode = 0;
  1159. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1160. if (err)
  1161. goto out;
  1162. dentry = fhp->fh_dentry;
  1163. dirp = dentry->d_inode;
  1164. /* Get all the sanity checks out of the way before
  1165. * we lock the parent. */
  1166. err = nfserr_notdir;
  1167. if(!dirp->i_op || !dirp->i_op->lookup)
  1168. goto out;
  1169. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1170. /*
  1171. * Compose the response file handle.
  1172. */
  1173. dchild = lookup_one_len(fname, dentry, flen);
  1174. host_err = PTR_ERR(dchild);
  1175. if (IS_ERR(dchild))
  1176. goto out_nfserr;
  1177. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1178. if (err)
  1179. goto out;
  1180. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1181. /* solaris7 gets confused (bugid 4218508) if these have
  1182. * the high bit set, so just clear the high bits. If this is
  1183. * ever changed to use different attrs for storing the
  1184. * verifier, then do_open_lookup() will also need to be fixed
  1185. * accordingly.
  1186. */
  1187. v_mtime = verifier[0]&0x7fffffff;
  1188. v_atime = verifier[1]&0x7fffffff;
  1189. }
  1190. if (dchild->d_inode) {
  1191. err = 0;
  1192. switch (createmode) {
  1193. case NFS3_CREATE_UNCHECKED:
  1194. if (! S_ISREG(dchild->d_inode->i_mode))
  1195. err = nfserr_exist;
  1196. else if (truncp) {
  1197. /* in nfsv4, we need to treat this case a little
  1198. * differently. we don't want to truncate the
  1199. * file now; this would be wrong if the OPEN
  1200. * fails for some other reason. furthermore,
  1201. * if the size is nonzero, we should ignore it
  1202. * according to spec!
  1203. */
  1204. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1205. }
  1206. else {
  1207. iap->ia_valid &= ATTR_SIZE;
  1208. goto set_attr;
  1209. }
  1210. break;
  1211. case NFS3_CREATE_EXCLUSIVE:
  1212. if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
  1213. && dchild->d_inode->i_atime.tv_sec == v_atime
  1214. && dchild->d_inode->i_size == 0 )
  1215. break;
  1216. /* fallthru */
  1217. case NFS3_CREATE_GUARDED:
  1218. err = nfserr_exist;
  1219. }
  1220. goto out;
  1221. }
  1222. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1223. if (host_err < 0)
  1224. goto out_nfserr;
  1225. if (created)
  1226. *created = 1;
  1227. if (EX_ISSYNC(fhp->fh_export)) {
  1228. err = nfserrno(nfsd_sync_dir(dentry));
  1229. /* setattr will sync the child (or not) */
  1230. }
  1231. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1232. /* Cram the verifier into atime/mtime */
  1233. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1234. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1235. /* XXX someone who knows this better please fix it for nsec */
  1236. iap->ia_mtime.tv_sec = v_mtime;
  1237. iap->ia_atime.tv_sec = v_atime;
  1238. iap->ia_mtime.tv_nsec = 0;
  1239. iap->ia_atime.tv_nsec = 0;
  1240. }
  1241. /* Set file attributes.
  1242. * Irix appears to send along the gid when it tries to
  1243. * implement setgid directories via NFS. Clear out all that cruft.
  1244. */
  1245. set_attr:
  1246. if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0) {
  1247. __be32 err2 = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1248. if (err2)
  1249. err = err2;
  1250. }
  1251. /*
  1252. * Update the filehandle to get the new inode info.
  1253. */
  1254. if (!err)
  1255. err = fh_update(resfhp);
  1256. out:
  1257. fh_unlock(fhp);
  1258. if (dchild && !IS_ERR(dchild))
  1259. dput(dchild);
  1260. return err;
  1261. out_nfserr:
  1262. err = nfserrno(host_err);
  1263. goto out;
  1264. }
  1265. #endif /* CONFIG_NFSD_V3 */
  1266. /*
  1267. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1268. * fits into the buffer. On return, it contains the true length.
  1269. * N.B. After this call fhp needs an fh_put
  1270. */
  1271. __be32
  1272. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1273. {
  1274. struct dentry *dentry;
  1275. struct inode *inode;
  1276. mm_segment_t oldfs;
  1277. __be32 err;
  1278. int host_err;
  1279. err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
  1280. if (err)
  1281. goto out;
  1282. dentry = fhp->fh_dentry;
  1283. inode = dentry->d_inode;
  1284. err = nfserr_inval;
  1285. if (!inode->i_op || !inode->i_op->readlink)
  1286. goto out;
  1287. touch_atime(fhp->fh_export->ex_mnt, dentry);
  1288. /* N.B. Why does this call need a get_fs()??
  1289. * Remove the set_fs and watch the fireworks:-) --okir
  1290. */
  1291. oldfs = get_fs(); set_fs(KERNEL_DS);
  1292. host_err = inode->i_op->readlink(dentry, buf, *lenp);
  1293. set_fs(oldfs);
  1294. if (host_err < 0)
  1295. goto out_nfserr;
  1296. *lenp = host_err;
  1297. err = 0;
  1298. out:
  1299. return err;
  1300. out_nfserr:
  1301. err = nfserrno(host_err);
  1302. goto out;
  1303. }
  1304. /*
  1305. * Create a symlink and look up its inode
  1306. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1307. */
  1308. __be32
  1309. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1310. char *fname, int flen,
  1311. char *path, int plen,
  1312. struct svc_fh *resfhp,
  1313. struct iattr *iap)
  1314. {
  1315. struct dentry *dentry, *dnew;
  1316. __be32 err, cerr;
  1317. int host_err;
  1318. umode_t mode;
  1319. err = nfserr_noent;
  1320. if (!flen || !plen)
  1321. goto out;
  1322. err = nfserr_exist;
  1323. if (isdotent(fname, flen))
  1324. goto out;
  1325. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1326. if (err)
  1327. goto out;
  1328. fh_lock(fhp);
  1329. dentry = fhp->fh_dentry;
  1330. dnew = lookup_one_len(fname, dentry, flen);
  1331. host_err = PTR_ERR(dnew);
  1332. if (IS_ERR(dnew))
  1333. goto out_nfserr;
  1334. mode = S_IALLUGO;
  1335. /* Only the MODE ATTRibute is even vaguely meaningful */
  1336. if (iap && (iap->ia_valid & ATTR_MODE))
  1337. mode = iap->ia_mode & S_IALLUGO;
  1338. if (unlikely(path[plen] != 0)) {
  1339. char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
  1340. if (path_alloced == NULL)
  1341. host_err = -ENOMEM;
  1342. else {
  1343. strncpy(path_alloced, path, plen);
  1344. path_alloced[plen] = 0;
  1345. host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced, mode);
  1346. kfree(path_alloced);
  1347. }
  1348. } else
  1349. host_err = vfs_symlink(dentry->d_inode, dnew, path, mode);
  1350. if (!host_err) {
  1351. if (EX_ISSYNC(fhp->fh_export))
  1352. host_err = nfsd_sync_dir(dentry);
  1353. }
  1354. err = nfserrno(host_err);
  1355. fh_unlock(fhp);
  1356. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1357. dput(dnew);
  1358. if (err==0) err = cerr;
  1359. out:
  1360. return err;
  1361. out_nfserr:
  1362. err = nfserrno(host_err);
  1363. goto out;
  1364. }
  1365. /*
  1366. * Create a hardlink
  1367. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1368. */
  1369. __be32
  1370. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1371. char *name, int len, struct svc_fh *tfhp)
  1372. {
  1373. struct dentry *ddir, *dnew, *dold;
  1374. struct inode *dirp, *dest;
  1375. __be32 err;
  1376. int host_err;
  1377. err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
  1378. if (err)
  1379. goto out;
  1380. err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
  1381. if (err)
  1382. goto out;
  1383. err = nfserr_perm;
  1384. if (!len)
  1385. goto out;
  1386. err = nfserr_exist;
  1387. if (isdotent(name, len))
  1388. goto out;
  1389. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1390. ddir = ffhp->fh_dentry;
  1391. dirp = ddir->d_inode;
  1392. dnew = lookup_one_len(name, ddir, len);
  1393. host_err = PTR_ERR(dnew);
  1394. if (IS_ERR(dnew))
  1395. goto out_nfserr;
  1396. dold = tfhp->fh_dentry;
  1397. dest = dold->d_inode;
  1398. host_err = vfs_link(dold, dirp, dnew);
  1399. if (!host_err) {
  1400. if (EX_ISSYNC(ffhp->fh_export)) {
  1401. err = nfserrno(nfsd_sync_dir(ddir));
  1402. write_inode_now(dest, 1);
  1403. }
  1404. err = 0;
  1405. } else {
  1406. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1407. err = nfserr_acces;
  1408. else
  1409. err = nfserrno(host_err);
  1410. }
  1411. dput(dnew);
  1412. out_unlock:
  1413. fh_unlock(ffhp);
  1414. out:
  1415. return err;
  1416. out_nfserr:
  1417. err = nfserrno(host_err);
  1418. goto out_unlock;
  1419. }
  1420. /*
  1421. * Rename a file
  1422. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1423. */
  1424. __be32
  1425. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1426. struct svc_fh *tfhp, char *tname, int tlen)
  1427. {
  1428. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1429. struct inode *fdir, *tdir;
  1430. __be32 err;
  1431. int host_err;
  1432. err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
  1433. if (err)
  1434. goto out;
  1435. err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
  1436. if (err)
  1437. goto out;
  1438. fdentry = ffhp->fh_dentry;
  1439. fdir = fdentry->d_inode;
  1440. tdentry = tfhp->fh_dentry;
  1441. tdir = tdentry->d_inode;
  1442. err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
  1443. if (ffhp->fh_export != tfhp->fh_export)
  1444. goto out;
  1445. err = nfserr_perm;
  1446. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1447. goto out;
  1448. /* cannot use fh_lock as we need deadlock protective ordering
  1449. * so do it by hand */
  1450. trap = lock_rename(tdentry, fdentry);
  1451. ffhp->fh_locked = tfhp->fh_locked = 1;
  1452. fill_pre_wcc(ffhp);
  1453. fill_pre_wcc(tfhp);
  1454. odentry = lookup_one_len(fname, fdentry, flen);
  1455. host_err = PTR_ERR(odentry);
  1456. if (IS_ERR(odentry))
  1457. goto out_nfserr;
  1458. host_err = -ENOENT;
  1459. if (!odentry->d_inode)
  1460. goto out_dput_old;
  1461. host_err = -EINVAL;
  1462. if (odentry == trap)
  1463. goto out_dput_old;
  1464. ndentry = lookup_one_len(tname, tdentry, tlen);
  1465. host_err = PTR_ERR(ndentry);
  1466. if (IS_ERR(ndentry))
  1467. goto out_dput_old;
  1468. host_err = -ENOTEMPTY;
  1469. if (ndentry == trap)
  1470. goto out_dput_new;
  1471. #ifdef MSNFS
  1472. if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  1473. ((atomic_read(&odentry->d_count) > 1)
  1474. || (atomic_read(&ndentry->d_count) > 1))) {
  1475. host_err = -EPERM;
  1476. } else
  1477. #endif
  1478. host_err = vfs_rename(fdir, odentry, tdir, ndentry);
  1479. if (!host_err && EX_ISSYNC(tfhp->fh_export)) {
  1480. host_err = nfsd_sync_dir(tdentry);
  1481. if (!host_err)
  1482. host_err = nfsd_sync_dir(fdentry);
  1483. }
  1484. out_dput_new:
  1485. dput(ndentry);
  1486. out_dput_old:
  1487. dput(odentry);
  1488. out_nfserr:
  1489. err = nfserrno(host_err);
  1490. /* we cannot reply on fh_unlock on the two filehandles,
  1491. * as that would do the wrong thing if the two directories
  1492. * were the same, so again we do it by hand
  1493. */
  1494. fill_post_wcc(ffhp);
  1495. fill_post_wcc(tfhp);
  1496. unlock_rename(tdentry, fdentry);
  1497. ffhp->fh_locked = tfhp->fh_locked = 0;
  1498. out:
  1499. return err;
  1500. }
  1501. /*
  1502. * Unlink a file or directory
  1503. * N.B. After this call fhp needs an fh_put
  1504. */
  1505. __be32
  1506. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1507. char *fname, int flen)
  1508. {
  1509. struct dentry *dentry, *rdentry;
  1510. struct inode *dirp;
  1511. __be32 err;
  1512. int host_err;
  1513. err = nfserr_acces;
  1514. if (!flen || isdotent(fname, flen))
  1515. goto out;
  1516. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
  1517. if (err)
  1518. goto out;
  1519. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1520. dentry = fhp->fh_dentry;
  1521. dirp = dentry->d_inode;
  1522. rdentry = lookup_one_len(fname, dentry, flen);
  1523. host_err = PTR_ERR(rdentry);
  1524. if (IS_ERR(rdentry))
  1525. goto out_nfserr;
  1526. if (!rdentry->d_inode) {
  1527. dput(rdentry);
  1528. err = nfserr_noent;
  1529. goto out;
  1530. }
  1531. if (!type)
  1532. type = rdentry->d_inode->i_mode & S_IFMT;
  1533. if (type != S_IFDIR) { /* It's UNLINK */
  1534. #ifdef MSNFS
  1535. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  1536. (atomic_read(&rdentry->d_count) > 1)) {
  1537. host_err = -EPERM;
  1538. } else
  1539. #endif
  1540. host_err = vfs_unlink(dirp, rdentry);
  1541. } else { /* It's RMDIR */
  1542. host_err = vfs_rmdir(dirp, rdentry);
  1543. }
  1544. dput(rdentry);
  1545. if (host_err)
  1546. goto out_nfserr;
  1547. if (EX_ISSYNC(fhp->fh_export))
  1548. host_err = nfsd_sync_dir(dentry);
  1549. out_nfserr:
  1550. err = nfserrno(host_err);
  1551. out:
  1552. return err;
  1553. }
  1554. /*
  1555. * Read entries from a directory.
  1556. * The NFSv3/4 verifier we ignore for now.
  1557. */
  1558. __be32
  1559. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1560. struct readdir_cd *cdp, filldir_t func)
  1561. {
  1562. __be32 err;
  1563. int host_err;
  1564. struct file *file;
  1565. loff_t offset = *offsetp;
  1566. err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
  1567. if (err)
  1568. goto out;
  1569. offset = vfs_llseek(file, offset, 0);
  1570. if (offset < 0) {
  1571. err = nfserrno((int)offset);
  1572. goto out_close;
  1573. }
  1574. /*
  1575. * Read the directory entries. This silly loop is necessary because
  1576. * readdir() is not guaranteed to fill up the entire buffer, but
  1577. * may choose to do less.
  1578. */
  1579. do {
  1580. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1581. host_err = vfs_readdir(file, func, cdp);
  1582. } while (host_err >=0 && cdp->err == nfs_ok);
  1583. if (host_err)
  1584. err = nfserrno(host_err);
  1585. else
  1586. err = cdp->err;
  1587. *offsetp = vfs_llseek(file, 0, 1);
  1588. if (err == nfserr_eof || err == nfserr_toosmall)
  1589. err = nfs_ok; /* can still be found in ->err */
  1590. out_close:
  1591. nfsd_close(file);
  1592. out:
  1593. return err;
  1594. }
  1595. /*
  1596. * Get file system stats
  1597. * N.B. After this call fhp needs an fh_put
  1598. */
  1599. __be32
  1600. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat)
  1601. {
  1602. __be32 err = fh_verify(rqstp, fhp, 0, MAY_NOP);
  1603. if (!err && vfs_statfs(fhp->fh_dentry,stat))
  1604. err = nfserr_io;
  1605. return err;
  1606. }
  1607. static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
  1608. {
  1609. return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
  1610. }
  1611. /*
  1612. * Check for a user's access permissions to this inode.
  1613. */
  1614. __be32
  1615. nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
  1616. struct dentry *dentry, int acc)
  1617. {
  1618. struct inode *inode = dentry->d_inode;
  1619. int err;
  1620. if (acc == MAY_NOP)
  1621. return 0;
  1622. #if 0
  1623. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1624. acc,
  1625. (acc & MAY_READ)? " read" : "",
  1626. (acc & MAY_WRITE)? " write" : "",
  1627. (acc & MAY_EXEC)? " exec" : "",
  1628. (acc & MAY_SATTR)? " sattr" : "",
  1629. (acc & MAY_TRUNC)? " trunc" : "",
  1630. (acc & MAY_LOCK)? " lock" : "",
  1631. (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1632. inode->i_mode,
  1633. IS_IMMUTABLE(inode)? " immut" : "",
  1634. IS_APPEND(inode)? " append" : "",
  1635. IS_RDONLY(inode)? " ro" : "");
  1636. dprintk(" owner %d/%d user %d/%d\n",
  1637. inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
  1638. #endif
  1639. /* Normally we reject any write/sattr etc access on a read-only file
  1640. * system. But if it is IRIX doing check on write-access for a
  1641. * device special file, we ignore rofs.
  1642. */
  1643. if (!(acc & MAY_LOCAL_ACCESS))
  1644. if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
  1645. if (exp_rdonly(rqstp, exp) || IS_RDONLY(inode))
  1646. return nfserr_rofs;
  1647. if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1648. return nfserr_perm;
  1649. }
  1650. if ((acc & MAY_TRUNC) && IS_APPEND(inode))
  1651. return nfserr_perm;
  1652. if (acc & MAY_LOCK) {
  1653. /* If we cannot rely on authentication in NLM requests,
  1654. * just allow locks, otherwise require read permission, or
  1655. * ownership
  1656. */
  1657. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1658. return 0;
  1659. else
  1660. acc = MAY_READ | MAY_OWNER_OVERRIDE;
  1661. }
  1662. /*
  1663. * The file owner always gets access permission for accesses that
  1664. * would normally be checked at open time. This is to make
  1665. * file access work even when the client has done a fchmod(fd, 0).
  1666. *
  1667. * However, `cp foo bar' should fail nevertheless when bar is
  1668. * readonly. A sensible way to do this might be to reject all
  1669. * attempts to truncate a read-only file, because a creat() call
  1670. * always implies file truncation.
  1671. * ... but this isn't really fair. A process may reasonably call
  1672. * ftruncate on an open file descriptor on a file with perm 000.
  1673. * We must trust the client to do permission checking - using "ACCESS"
  1674. * with NFSv3.
  1675. */
  1676. if ((acc & MAY_OWNER_OVERRIDE) &&
  1677. inode->i_uid == current->fsuid)
  1678. return 0;
  1679. err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC), NULL);
  1680. /* Allow read access to binaries even when mode 111 */
  1681. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1682. acc == (MAY_READ | MAY_OWNER_OVERRIDE))
  1683. err = permission(inode, MAY_EXEC, NULL);
  1684. return err? nfserrno(err) : 0;
  1685. }
  1686. void
  1687. nfsd_racache_shutdown(void)
  1688. {
  1689. if (!raparml)
  1690. return;
  1691. dprintk("nfsd: freeing readahead buffers.\n");
  1692. kfree(raparml);
  1693. raparml = NULL;
  1694. }
  1695. /*
  1696. * Initialize readahead param cache
  1697. */
  1698. int
  1699. nfsd_racache_init(int cache_size)
  1700. {
  1701. int i;
  1702. int j = 0;
  1703. int nperbucket;
  1704. if (raparml)
  1705. return 0;
  1706. if (cache_size < 2*RAPARM_HASH_SIZE)
  1707. cache_size = 2*RAPARM_HASH_SIZE;
  1708. raparml = kcalloc(cache_size, sizeof(struct raparms), GFP_KERNEL);
  1709. if (!raparml) {
  1710. printk(KERN_WARNING
  1711. "nfsd: Could not allocate memory read-ahead cache.\n");
  1712. return -ENOMEM;
  1713. }
  1714. dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
  1715. for (i = 0 ; i < RAPARM_HASH_SIZE ; i++) {
  1716. raparm_hash[i].pb_head = NULL;
  1717. spin_lock_init(&raparm_hash[i].pb_lock);
  1718. }
  1719. nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
  1720. for (i = 0; i < cache_size - 1; i++) {
  1721. if (i % nperbucket == 0)
  1722. raparm_hash[j++].pb_head = raparml + i;
  1723. if (i % nperbucket < nperbucket-1)
  1724. raparml[i].p_next = raparml + i + 1;
  1725. }
  1726. nfsdstats.ra_size = cache_size;
  1727. return 0;
  1728. }
  1729. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  1730. struct posix_acl *
  1731. nfsd_get_posix_acl(struct svc_fh *fhp, int type)
  1732. {
  1733. struct inode *inode = fhp->fh_dentry->d_inode;
  1734. char *name;
  1735. void *value = NULL;
  1736. ssize_t size;
  1737. struct posix_acl *acl;
  1738. if (!IS_POSIXACL(inode))
  1739. return ERR_PTR(-EOPNOTSUPP);
  1740. switch (type) {
  1741. case ACL_TYPE_ACCESS:
  1742. name = POSIX_ACL_XATTR_ACCESS;
  1743. break;
  1744. case ACL_TYPE_DEFAULT:
  1745. name = POSIX_ACL_XATTR_DEFAULT;
  1746. break;
  1747. default:
  1748. return ERR_PTR(-EOPNOTSUPP);
  1749. }
  1750. size = nfsd_getxattr(fhp->fh_dentry, name, &value);
  1751. if (size < 0)
  1752. return ERR_PTR(size);
  1753. acl = posix_acl_from_xattr(value, size);
  1754. kfree(value);
  1755. return acl;
  1756. }
  1757. int
  1758. nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
  1759. {
  1760. struct inode *inode = fhp->fh_dentry->d_inode;
  1761. char *name;
  1762. void *value = NULL;
  1763. size_t size;
  1764. int error;
  1765. if (!IS_POSIXACL(inode) || !inode->i_op ||
  1766. !inode->i_op->setxattr || !inode->i_op->removexattr)
  1767. return -EOPNOTSUPP;
  1768. switch(type) {
  1769. case ACL_TYPE_ACCESS:
  1770. name = POSIX_ACL_XATTR_ACCESS;
  1771. break;
  1772. case ACL_TYPE_DEFAULT:
  1773. name = POSIX_ACL_XATTR_DEFAULT;
  1774. break;
  1775. default:
  1776. return -EOPNOTSUPP;
  1777. }
  1778. if (acl && acl->a_count) {
  1779. size = posix_acl_xattr_size(acl->a_count);
  1780. value = kmalloc(size, GFP_KERNEL);
  1781. if (!value)
  1782. return -ENOMEM;
  1783. error = posix_acl_to_xattr(acl, value, size);
  1784. if (error < 0)
  1785. goto getout;
  1786. size = error;
  1787. } else
  1788. size = 0;
  1789. if (size)
  1790. error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
  1791. else {
  1792. if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
  1793. error = 0;
  1794. else {
  1795. error = vfs_removexattr(fhp->fh_dentry, name);
  1796. if (error == -ENODATA)
  1797. error = 0;
  1798. }
  1799. }
  1800. getout:
  1801. kfree(value);
  1802. return error;
  1803. }
  1804. #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */