vfs.c 46 KB

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