vfs.c 47 KB

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