dir.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  1. /*
  2. * linux/fs/nfs/dir.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs directory handling functions
  7. *
  8. * 10 Apr 1996 Added silly rename for unlink --okir
  9. * 28 Sep 1996 Improved directory cache --okir
  10. * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
  11. * Re-implemented silly rename for unlink, newly implemented
  12. * silly rename for nfs_rename() following the suggestions
  13. * of Olaf Kirch (okir) found in this file.
  14. * Following Linus comments on my original hack, this version
  15. * depends only on the dcache stuff and doesn't touch the inode
  16. * layer (iput() and friends).
  17. * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
  18. */
  19. #include <linux/time.h>
  20. #include <linux/errno.h>
  21. #include <linux/stat.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/string.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/sunrpc/clnt.h>
  28. #include <linux/nfs_fs.h>
  29. #include <linux/nfs_mount.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/pagevec.h>
  33. #include <linux/namei.h>
  34. #include <linux/mount.h>
  35. #include <linux/sched.h>
  36. #include "nfs4_fs.h"
  37. #include "delegation.h"
  38. #include "iostat.h"
  39. /* #define NFS_DEBUG_VERBOSE 1 */
  40. static int nfs_opendir(struct inode *, struct file *);
  41. static int nfs_readdir(struct file *, void *, filldir_t);
  42. static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
  43. static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
  44. static int nfs_mkdir(struct inode *, struct dentry *, int);
  45. static int nfs_rmdir(struct inode *, struct dentry *);
  46. static int nfs_unlink(struct inode *, struct dentry *);
  47. static int nfs_symlink(struct inode *, struct dentry *, const char *);
  48. static int nfs_link(struct dentry *, struct inode *, struct dentry *);
  49. static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
  50. static int nfs_rename(struct inode *, struct dentry *,
  51. struct inode *, struct dentry *);
  52. static int nfs_fsync_dir(struct file *, struct dentry *, int);
  53. static loff_t nfs_llseek_dir(struct file *, loff_t, int);
  54. const struct file_operations nfs_dir_operations = {
  55. .llseek = nfs_llseek_dir,
  56. .read = generic_read_dir,
  57. .readdir = nfs_readdir,
  58. .open = nfs_opendir,
  59. .release = nfs_release,
  60. .fsync = nfs_fsync_dir,
  61. };
  62. const struct inode_operations nfs_dir_inode_operations = {
  63. .create = nfs_create,
  64. .lookup = nfs_lookup,
  65. .link = nfs_link,
  66. .unlink = nfs_unlink,
  67. .symlink = nfs_symlink,
  68. .mkdir = nfs_mkdir,
  69. .rmdir = nfs_rmdir,
  70. .mknod = nfs_mknod,
  71. .rename = nfs_rename,
  72. .permission = nfs_permission,
  73. .getattr = nfs_getattr,
  74. .setattr = nfs_setattr,
  75. };
  76. #ifdef CONFIG_NFS_V3
  77. const struct inode_operations nfs3_dir_inode_operations = {
  78. .create = nfs_create,
  79. .lookup = nfs_lookup,
  80. .link = nfs_link,
  81. .unlink = nfs_unlink,
  82. .symlink = nfs_symlink,
  83. .mkdir = nfs_mkdir,
  84. .rmdir = nfs_rmdir,
  85. .mknod = nfs_mknod,
  86. .rename = nfs_rename,
  87. .permission = nfs_permission,
  88. .getattr = nfs_getattr,
  89. .setattr = nfs_setattr,
  90. .listxattr = nfs3_listxattr,
  91. .getxattr = nfs3_getxattr,
  92. .setxattr = nfs3_setxattr,
  93. .removexattr = nfs3_removexattr,
  94. };
  95. #endif /* CONFIG_NFS_V3 */
  96. #ifdef CONFIG_NFS_V4
  97. static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
  98. const struct inode_operations nfs4_dir_inode_operations = {
  99. .create = nfs_create,
  100. .lookup = nfs_atomic_lookup,
  101. .link = nfs_link,
  102. .unlink = nfs_unlink,
  103. .symlink = nfs_symlink,
  104. .mkdir = nfs_mkdir,
  105. .rmdir = nfs_rmdir,
  106. .mknod = nfs_mknod,
  107. .rename = nfs_rename,
  108. .permission = nfs_permission,
  109. .getattr = nfs_getattr,
  110. .setattr = nfs_setattr,
  111. .getxattr = nfs4_getxattr,
  112. .setxattr = nfs4_setxattr,
  113. .listxattr = nfs4_listxattr,
  114. };
  115. #endif /* CONFIG_NFS_V4 */
  116. /*
  117. * Open file
  118. */
  119. static int
  120. nfs_opendir(struct inode *inode, struct file *filp)
  121. {
  122. int res;
  123. dfprintk(VFS, "NFS: opendir(%s/%ld)\n",
  124. inode->i_sb->s_id, inode->i_ino);
  125. lock_kernel();
  126. /* Call generic open code in order to cache credentials */
  127. res = nfs_open(inode, filp);
  128. unlock_kernel();
  129. return res;
  130. }
  131. typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int);
  132. typedef struct {
  133. struct file *file;
  134. struct page *page;
  135. unsigned long page_index;
  136. __be32 *ptr;
  137. u64 *dir_cookie;
  138. loff_t current_index;
  139. struct nfs_entry *entry;
  140. decode_dirent_t decode;
  141. int plus;
  142. int error;
  143. unsigned long timestamp;
  144. int timestamp_valid;
  145. } nfs_readdir_descriptor_t;
  146. /* Now we cache directories properly, by stuffing the dirent
  147. * data directly in the page cache.
  148. *
  149. * Inode invalidation due to refresh etc. takes care of
  150. * _everything_, no sloppy entry flushing logic, no extraneous
  151. * copying, network direct to page cache, the way it was meant
  152. * to be.
  153. *
  154. * NOTE: Dirent information verification is done always by the
  155. * page-in of the RPC reply, nowhere else, this simplies
  156. * things substantially.
  157. */
  158. static
  159. int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
  160. {
  161. struct file *file = desc->file;
  162. struct inode *inode = file->f_path.dentry->d_inode;
  163. struct rpc_cred *cred = nfs_file_cred(file);
  164. unsigned long timestamp;
  165. int error;
  166. dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n",
  167. __FUNCTION__, (long long)desc->entry->cookie,
  168. page->index);
  169. again:
  170. timestamp = jiffies;
  171. error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page,
  172. NFS_SERVER(inode)->dtsize, desc->plus);
  173. if (error < 0) {
  174. /* We requested READDIRPLUS, but the server doesn't grok it */
  175. if (error == -ENOTSUPP && desc->plus) {
  176. NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
  177. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
  178. desc->plus = 0;
  179. goto again;
  180. }
  181. goto error;
  182. }
  183. desc->timestamp = timestamp;
  184. desc->timestamp_valid = 1;
  185. SetPageUptodate(page);
  186. spin_lock(&inode->i_lock);
  187. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
  188. spin_unlock(&inode->i_lock);
  189. /* Ensure consistent page alignment of the data.
  190. * Note: assumes we have exclusive access to this mapping either
  191. * through inode->i_mutex or some other mechanism.
  192. */
  193. if (page->index == 0 && invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1) < 0) {
  194. /* Should never happen */
  195. nfs_zap_mapping(inode, inode->i_mapping);
  196. }
  197. unlock_page(page);
  198. return 0;
  199. error:
  200. SetPageError(page);
  201. unlock_page(page);
  202. nfs_zap_caches(inode);
  203. desc->error = error;
  204. return -EIO;
  205. }
  206. static inline
  207. int dir_decode(nfs_readdir_descriptor_t *desc)
  208. {
  209. __be32 *p = desc->ptr;
  210. p = desc->decode(p, desc->entry, desc->plus);
  211. if (IS_ERR(p))
  212. return PTR_ERR(p);
  213. desc->ptr = p;
  214. if (desc->timestamp_valid)
  215. desc->entry->fattr->time_start = desc->timestamp;
  216. else
  217. desc->entry->fattr->valid &= ~NFS_ATTR_FATTR;
  218. return 0;
  219. }
  220. static inline
  221. void dir_page_release(nfs_readdir_descriptor_t *desc)
  222. {
  223. kunmap(desc->page);
  224. page_cache_release(desc->page);
  225. desc->page = NULL;
  226. desc->ptr = NULL;
  227. }
  228. /*
  229. * Given a pointer to a buffer that has already been filled by a call
  230. * to readdir, find the next entry with cookie '*desc->dir_cookie'.
  231. *
  232. * If the end of the buffer has been reached, return -EAGAIN, if not,
  233. * return the offset within the buffer of the next entry to be
  234. * read.
  235. */
  236. static inline
  237. int find_dirent(nfs_readdir_descriptor_t *desc)
  238. {
  239. struct nfs_entry *entry = desc->entry;
  240. int loop_count = 0,
  241. status;
  242. while((status = dir_decode(desc)) == 0) {
  243. dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n",
  244. __FUNCTION__, (unsigned long long)entry->cookie);
  245. if (entry->prev_cookie == *desc->dir_cookie)
  246. break;
  247. if (loop_count++ > 200) {
  248. loop_count = 0;
  249. schedule();
  250. }
  251. }
  252. return status;
  253. }
  254. /*
  255. * Given a pointer to a buffer that has already been filled by a call
  256. * to readdir, find the entry at offset 'desc->file->f_pos'.
  257. *
  258. * If the end of the buffer has been reached, return -EAGAIN, if not,
  259. * return the offset within the buffer of the next entry to be
  260. * read.
  261. */
  262. static inline
  263. int find_dirent_index(nfs_readdir_descriptor_t *desc)
  264. {
  265. struct nfs_entry *entry = desc->entry;
  266. int loop_count = 0,
  267. status;
  268. for(;;) {
  269. status = dir_decode(desc);
  270. if (status)
  271. break;
  272. dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n",
  273. (unsigned long long)entry->cookie, desc->current_index);
  274. if (desc->file->f_pos == desc->current_index) {
  275. *desc->dir_cookie = entry->cookie;
  276. break;
  277. }
  278. desc->current_index++;
  279. if (loop_count++ > 200) {
  280. loop_count = 0;
  281. schedule();
  282. }
  283. }
  284. return status;
  285. }
  286. /*
  287. * Find the given page, and call find_dirent() or find_dirent_index in
  288. * order to try to return the next entry.
  289. */
  290. static inline
  291. int find_dirent_page(nfs_readdir_descriptor_t *desc)
  292. {
  293. struct inode *inode = desc->file->f_path.dentry->d_inode;
  294. struct page *page;
  295. int status;
  296. dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n",
  297. __FUNCTION__, desc->page_index,
  298. (long long) *desc->dir_cookie);
  299. /* If we find the page in the page_cache, we cannot be sure
  300. * how fresh the data is, so we will ignore readdir_plus attributes.
  301. */
  302. desc->timestamp_valid = 0;
  303. page = read_cache_page(inode->i_mapping, desc->page_index,
  304. (filler_t *)nfs_readdir_filler, desc);
  305. if (IS_ERR(page)) {
  306. status = PTR_ERR(page);
  307. goto out;
  308. }
  309. /* NOTE: Someone else may have changed the READDIRPLUS flag */
  310. desc->page = page;
  311. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  312. if (*desc->dir_cookie != 0)
  313. status = find_dirent(desc);
  314. else
  315. status = find_dirent_index(desc);
  316. if (status < 0)
  317. dir_page_release(desc);
  318. out:
  319. dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, status);
  320. return status;
  321. }
  322. /*
  323. * Recurse through the page cache pages, and return a
  324. * filled nfs_entry structure of the next directory entry if possible.
  325. *
  326. * The target for the search is '*desc->dir_cookie' if non-0,
  327. * 'desc->file->f_pos' otherwise
  328. */
  329. static inline
  330. int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  331. {
  332. int loop_count = 0;
  333. int res;
  334. /* Always search-by-index from the beginning of the cache */
  335. if (*desc->dir_cookie == 0) {
  336. dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
  337. (long long)desc->file->f_pos);
  338. desc->page_index = 0;
  339. desc->entry->cookie = desc->entry->prev_cookie = 0;
  340. desc->entry->eof = 0;
  341. desc->current_index = 0;
  342. } else
  343. dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
  344. (unsigned long long)*desc->dir_cookie);
  345. for (;;) {
  346. res = find_dirent_page(desc);
  347. if (res != -EAGAIN)
  348. break;
  349. /* Align to beginning of next page */
  350. desc->page_index ++;
  351. if (loop_count++ > 200) {
  352. loop_count = 0;
  353. schedule();
  354. }
  355. }
  356. dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, res);
  357. return res;
  358. }
  359. static inline unsigned int dt_type(struct inode *inode)
  360. {
  361. return (inode->i_mode >> 12) & 15;
  362. }
  363. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
  364. /*
  365. * Once we've found the start of the dirent within a page: fill 'er up...
  366. */
  367. static
  368. int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
  369. filldir_t filldir)
  370. {
  371. struct file *file = desc->file;
  372. struct nfs_entry *entry = desc->entry;
  373. struct dentry *dentry = NULL;
  374. u64 fileid;
  375. int loop_count = 0,
  376. res;
  377. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
  378. (unsigned long long)entry->cookie);
  379. for(;;) {
  380. unsigned d_type = DT_UNKNOWN;
  381. /* Note: entry->prev_cookie contains the cookie for
  382. * retrieving the current dirent on the server */
  383. fileid = entry->ino;
  384. /* Get a dentry if we have one */
  385. if (dentry != NULL)
  386. dput(dentry);
  387. dentry = nfs_readdir_lookup(desc);
  388. /* Use readdirplus info */
  389. if (dentry != NULL && dentry->d_inode != NULL) {
  390. d_type = dt_type(dentry->d_inode);
  391. fileid = NFS_FILEID(dentry->d_inode);
  392. }
  393. res = filldir(dirent, entry->name, entry->len,
  394. file->f_pos, fileid, d_type);
  395. if (res < 0)
  396. break;
  397. file->f_pos++;
  398. *desc->dir_cookie = entry->cookie;
  399. if (dir_decode(desc) != 0) {
  400. desc->page_index ++;
  401. break;
  402. }
  403. if (loop_count++ > 200) {
  404. loop_count = 0;
  405. schedule();
  406. }
  407. }
  408. dir_page_release(desc);
  409. if (dentry != NULL)
  410. dput(dentry);
  411. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
  412. (unsigned long long)*desc->dir_cookie, res);
  413. return res;
  414. }
  415. /*
  416. * If we cannot find a cookie in our cache, we suspect that this is
  417. * because it points to a deleted file, so we ask the server to return
  418. * whatever it thinks is the next entry. We then feed this to filldir.
  419. * If all goes well, we should then be able to find our way round the
  420. * cache on the next call to readdir_search_pagecache();
  421. *
  422. * NOTE: we cannot add the anonymous page to the pagecache because
  423. * the data it contains might not be page aligned. Besides,
  424. * we should already have a complete representation of the
  425. * directory in the page cache by the time we get here.
  426. */
  427. static inline
  428. int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
  429. filldir_t filldir)
  430. {
  431. struct file *file = desc->file;
  432. struct inode *inode = file->f_path.dentry->d_inode;
  433. struct rpc_cred *cred = nfs_file_cred(file);
  434. struct page *page = NULL;
  435. int status;
  436. unsigned long timestamp;
  437. dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
  438. (unsigned long long)*desc->dir_cookie);
  439. page = alloc_page(GFP_HIGHUSER);
  440. if (!page) {
  441. status = -ENOMEM;
  442. goto out;
  443. }
  444. timestamp = jiffies;
  445. desc->error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, *desc->dir_cookie,
  446. page,
  447. NFS_SERVER(inode)->dtsize,
  448. desc->plus);
  449. spin_lock(&inode->i_lock);
  450. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
  451. spin_unlock(&inode->i_lock);
  452. desc->page = page;
  453. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  454. if (desc->error >= 0) {
  455. desc->timestamp = timestamp;
  456. desc->timestamp_valid = 1;
  457. if ((status = dir_decode(desc)) == 0)
  458. desc->entry->prev_cookie = *desc->dir_cookie;
  459. } else
  460. status = -EIO;
  461. if (status < 0)
  462. goto out_release;
  463. status = nfs_do_filldir(desc, dirent, filldir);
  464. /* Reset read descriptor so it searches the page cache from
  465. * the start upon the next call to readdir_search_pagecache() */
  466. desc->page_index = 0;
  467. desc->entry->cookie = desc->entry->prev_cookie = 0;
  468. desc->entry->eof = 0;
  469. out:
  470. dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
  471. __FUNCTION__, status);
  472. return status;
  473. out_release:
  474. dir_page_release(desc);
  475. goto out;
  476. }
  477. /* The file offset position represents the dirent entry number. A
  478. last cookie cache takes care of the common case of reading the
  479. whole directory.
  480. */
  481. static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  482. {
  483. struct dentry *dentry = filp->f_path.dentry;
  484. struct inode *inode = dentry->d_inode;
  485. nfs_readdir_descriptor_t my_desc,
  486. *desc = &my_desc;
  487. struct nfs_entry my_entry;
  488. struct nfs_fh fh;
  489. struct nfs_fattr fattr;
  490. long res;
  491. dfprintk(VFS, "NFS: readdir(%s/%s) starting at cookie %Lu\n",
  492. dentry->d_parent->d_name.name, dentry->d_name.name,
  493. (long long)filp->f_pos);
  494. nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
  495. lock_kernel();
  496. res = nfs_revalidate_mapping_nolock(inode, filp->f_mapping);
  497. if (res < 0) {
  498. unlock_kernel();
  499. return res;
  500. }
  501. /*
  502. * filp->f_pos points to the dirent entry number.
  503. * *desc->dir_cookie has the cookie for the next entry. We have
  504. * to either find the entry with the appropriate number or
  505. * revalidate the cookie.
  506. */
  507. memset(desc, 0, sizeof(*desc));
  508. desc->file = filp;
  509. desc->dir_cookie = &((struct nfs_open_context *)filp->private_data)->dir_cookie;
  510. desc->decode = NFS_PROTO(inode)->decode_dirent;
  511. desc->plus = NFS_USE_READDIRPLUS(inode);
  512. my_entry.cookie = my_entry.prev_cookie = 0;
  513. my_entry.eof = 0;
  514. my_entry.fh = &fh;
  515. my_entry.fattr = &fattr;
  516. nfs_fattr_init(&fattr);
  517. desc->entry = &my_entry;
  518. while(!desc->entry->eof) {
  519. res = readdir_search_pagecache(desc);
  520. if (res == -EBADCOOKIE) {
  521. /* This means either end of directory */
  522. if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
  523. /* Or that the server has 'lost' a cookie */
  524. res = uncached_readdir(desc, dirent, filldir);
  525. if (res >= 0)
  526. continue;
  527. }
  528. res = 0;
  529. break;
  530. }
  531. if (res == -ETOOSMALL && desc->plus) {
  532. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
  533. nfs_zap_caches(inode);
  534. desc->plus = 0;
  535. desc->entry->eof = 0;
  536. continue;
  537. }
  538. if (res < 0)
  539. break;
  540. res = nfs_do_filldir(desc, dirent, filldir);
  541. if (res < 0) {
  542. res = 0;
  543. break;
  544. }
  545. }
  546. unlock_kernel();
  547. if (res > 0)
  548. res = 0;
  549. dfprintk(VFS, "NFS: readdir(%s/%s) returns %ld\n",
  550. dentry->d_parent->d_name.name, dentry->d_name.name,
  551. res);
  552. return res;
  553. }
  554. static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
  555. {
  556. mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
  557. switch (origin) {
  558. case 1:
  559. offset += filp->f_pos;
  560. case 0:
  561. if (offset >= 0)
  562. break;
  563. default:
  564. offset = -EINVAL;
  565. goto out;
  566. }
  567. if (offset != filp->f_pos) {
  568. filp->f_pos = offset;
  569. ((struct nfs_open_context *)filp->private_data)->dir_cookie = 0;
  570. }
  571. out:
  572. mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
  573. return offset;
  574. }
  575. /*
  576. * All directory operations under NFS are synchronous, so fsync()
  577. * is a dummy operation.
  578. */
  579. static int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
  580. {
  581. dfprintk(VFS, "NFS: fsync_dir(%s/%s) datasync %d\n",
  582. dentry->d_parent->d_name.name, dentry->d_name.name,
  583. datasync);
  584. return 0;
  585. }
  586. /*
  587. * A check for whether or not the parent directory has changed.
  588. * In the case it has, we assume that the dentries are untrustworthy
  589. * and may need to be looked up again.
  590. */
  591. static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
  592. {
  593. unsigned long verf;
  594. if (IS_ROOT(dentry))
  595. return 1;
  596. verf = dentry->d_time;
  597. if (nfs_caches_unstable(dir)
  598. || verf != NFS_I(dir)->cache_change_attribute)
  599. return 0;
  600. return 1;
  601. }
  602. static inline void nfs_set_verifier(struct dentry * dentry, unsigned long verf)
  603. {
  604. dentry->d_time = verf;
  605. }
  606. static void nfs_refresh_verifier(struct dentry * dentry, unsigned long verf)
  607. {
  608. nfs_set_verifier(dentry, verf);
  609. }
  610. /*
  611. * Return the intent data that applies to this particular path component
  612. *
  613. * Note that the current set of intents only apply to the very last
  614. * component of the path.
  615. * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
  616. */
  617. static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
  618. {
  619. if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
  620. return 0;
  621. return nd->flags & mask;
  622. }
  623. /*
  624. * Inode and filehandle revalidation for lookups.
  625. *
  626. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  627. * or if the intent information indicates that we're about to open this
  628. * particular file and the "nocto" mount flag is not set.
  629. *
  630. */
  631. static inline
  632. int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
  633. {
  634. struct nfs_server *server = NFS_SERVER(inode);
  635. if (nd != NULL) {
  636. /* VFS wants an on-the-wire revalidation */
  637. if (nd->flags & LOOKUP_REVAL)
  638. goto out_force;
  639. /* This is an open(2) */
  640. if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
  641. !(server->flags & NFS_MOUNT_NOCTO) &&
  642. (S_ISREG(inode->i_mode) ||
  643. S_ISDIR(inode->i_mode)))
  644. goto out_force;
  645. }
  646. return nfs_revalidate_inode(server, inode);
  647. out_force:
  648. return __nfs_revalidate_inode(server, inode);
  649. }
  650. /*
  651. * We judge how long we want to trust negative
  652. * dentries by looking at the parent inode mtime.
  653. *
  654. * If parent mtime has changed, we revalidate, else we wait for a
  655. * period corresponding to the parent's attribute cache timeout value.
  656. */
  657. static inline
  658. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  659. struct nameidata *nd)
  660. {
  661. /* Don't revalidate a negative dentry if we're creating a new file */
  662. if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
  663. return 0;
  664. return !nfs_check_verifier(dir, dentry);
  665. }
  666. /*
  667. * This is called every time the dcache has a lookup hit,
  668. * and we should check whether we can really trust that
  669. * lookup.
  670. *
  671. * NOTE! The hit can be a negative hit too, don't assume
  672. * we have an inode!
  673. *
  674. * If the parent directory is seen to have changed, we throw out the
  675. * cached dentry and do a new lookup.
  676. */
  677. static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
  678. {
  679. struct inode *dir;
  680. struct inode *inode;
  681. struct dentry *parent;
  682. int error;
  683. struct nfs_fh fhandle;
  684. struct nfs_fattr fattr;
  685. unsigned long verifier;
  686. parent = dget_parent(dentry);
  687. lock_kernel();
  688. dir = parent->d_inode;
  689. nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
  690. inode = dentry->d_inode;
  691. /* Revalidate parent directory attribute cache */
  692. if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
  693. goto out_zap_parent;
  694. if (!inode) {
  695. if (nfs_neg_need_reval(dir, dentry, nd))
  696. goto out_bad;
  697. goto out_valid;
  698. }
  699. if (is_bad_inode(inode)) {
  700. dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
  701. __FUNCTION__, dentry->d_parent->d_name.name,
  702. dentry->d_name.name);
  703. goto out_bad;
  704. }
  705. /* Force a full look up iff the parent directory has changed */
  706. if (nfs_check_verifier(dir, dentry)) {
  707. if (nfs_lookup_verify_inode(inode, nd))
  708. goto out_zap_parent;
  709. goto out_valid;
  710. }
  711. if (NFS_STALE(inode))
  712. goto out_bad;
  713. verifier = nfs_save_change_attribute(dir);
  714. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  715. if (error)
  716. goto out_bad;
  717. if (nfs_compare_fh(NFS_FH(inode), &fhandle))
  718. goto out_bad;
  719. if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
  720. goto out_bad;
  721. nfs_refresh_verifier(dentry, verifier);
  722. out_valid:
  723. unlock_kernel();
  724. dput(parent);
  725. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
  726. __FUNCTION__, dentry->d_parent->d_name.name,
  727. dentry->d_name.name);
  728. return 1;
  729. out_zap_parent:
  730. nfs_zap_caches(dir);
  731. out_bad:
  732. NFS_CACHEINV(dir);
  733. if (inode && S_ISDIR(inode->i_mode)) {
  734. /* Purge readdir caches. */
  735. nfs_zap_caches(inode);
  736. /* If we have submounts, don't unhash ! */
  737. if (have_submounts(dentry))
  738. goto out_valid;
  739. shrink_dcache_parent(dentry);
  740. }
  741. d_drop(dentry);
  742. unlock_kernel();
  743. dput(parent);
  744. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
  745. __FUNCTION__, dentry->d_parent->d_name.name,
  746. dentry->d_name.name);
  747. return 0;
  748. }
  749. /*
  750. * This is called from dput() when d_count is going to 0.
  751. */
  752. static int nfs_dentry_delete(struct dentry *dentry)
  753. {
  754. dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
  755. dentry->d_parent->d_name.name, dentry->d_name.name,
  756. dentry->d_flags);
  757. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  758. /* Unhash it, so that ->d_iput() would be called */
  759. return 1;
  760. }
  761. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  762. /* Unhash it, so that ancestors of killed async unlink
  763. * files will be cleaned up during umount */
  764. return 1;
  765. }
  766. return 0;
  767. }
  768. /*
  769. * Called when the dentry loses inode.
  770. * We use it to clean up silly-renamed files.
  771. */
  772. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  773. {
  774. nfs_inode_return_delegation(inode);
  775. if (S_ISDIR(inode->i_mode))
  776. /* drop any readdir cache as it could easily be old */
  777. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  778. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  779. lock_kernel();
  780. drop_nlink(inode);
  781. nfs_complete_unlink(dentry, inode);
  782. unlock_kernel();
  783. }
  784. iput(inode);
  785. }
  786. struct dentry_operations nfs_dentry_operations = {
  787. .d_revalidate = nfs_lookup_revalidate,
  788. .d_delete = nfs_dentry_delete,
  789. .d_iput = nfs_dentry_iput,
  790. };
  791. /*
  792. * Use intent information to check whether or not we're going to do
  793. * an O_EXCL create using this path component.
  794. */
  795. static inline
  796. int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
  797. {
  798. if (NFS_PROTO(dir)->version == 2)
  799. return 0;
  800. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0)
  801. return 0;
  802. return (nd->intent.open.flags & O_EXCL) != 0;
  803. }
  804. static inline int nfs_reval_fsid(struct inode *dir, const struct nfs_fattr *fattr)
  805. {
  806. struct nfs_server *server = NFS_SERVER(dir);
  807. if (!nfs_fsid_equal(&server->fsid, &fattr->fsid))
  808. /* Revalidate fsid using the parent directory */
  809. return __nfs_revalidate_inode(server, dir);
  810. return 0;
  811. }
  812. static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  813. {
  814. struct dentry *res;
  815. struct inode *inode = NULL;
  816. int error;
  817. struct nfs_fh fhandle;
  818. struct nfs_fattr fattr;
  819. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  820. dentry->d_parent->d_name.name, dentry->d_name.name);
  821. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  822. res = ERR_PTR(-ENAMETOOLONG);
  823. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  824. goto out;
  825. res = ERR_PTR(-ENOMEM);
  826. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  827. lock_kernel();
  828. /*
  829. * If we're doing an exclusive create, optimize away the lookup
  830. * but don't hash the dentry.
  831. */
  832. if (nfs_is_exclusive_create(dir, nd)) {
  833. d_instantiate(dentry, NULL);
  834. res = NULL;
  835. goto out_unlock;
  836. }
  837. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  838. if (error == -ENOENT)
  839. goto no_entry;
  840. if (error < 0) {
  841. res = ERR_PTR(error);
  842. goto out_unlock;
  843. }
  844. error = nfs_reval_fsid(dir, &fattr);
  845. if (error < 0) {
  846. res = ERR_PTR(error);
  847. goto out_unlock;
  848. }
  849. inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
  850. res = (struct dentry *)inode;
  851. if (IS_ERR(res))
  852. goto out_unlock;
  853. no_entry:
  854. res = d_materialise_unique(dentry, inode);
  855. if (res != NULL) {
  856. struct dentry *parent;
  857. if (IS_ERR(res))
  858. goto out_unlock;
  859. /* Was a directory renamed! */
  860. parent = dget_parent(res);
  861. if (!IS_ROOT(parent))
  862. nfs_mark_for_revalidate(parent->d_inode);
  863. dput(parent);
  864. dentry = res;
  865. }
  866. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  867. out_unlock:
  868. unlock_kernel();
  869. out:
  870. return res;
  871. }
  872. #ifdef CONFIG_NFS_V4
  873. static int nfs_open_revalidate(struct dentry *, struct nameidata *);
  874. struct dentry_operations nfs4_dentry_operations = {
  875. .d_revalidate = nfs_open_revalidate,
  876. .d_delete = nfs_dentry_delete,
  877. .d_iput = nfs_dentry_iput,
  878. };
  879. /*
  880. * Use intent information to determine whether we need to substitute
  881. * the NFSv4-style stateful OPEN for the LOOKUP call
  882. */
  883. static int is_atomic_open(struct inode *dir, struct nameidata *nd)
  884. {
  885. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
  886. return 0;
  887. /* NFS does not (yet) have a stateful open for directories */
  888. if (nd->flags & LOOKUP_DIRECTORY)
  889. return 0;
  890. /* Are we trying to write to a read only partition? */
  891. if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
  892. return 0;
  893. return 1;
  894. }
  895. static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  896. {
  897. struct dentry *res = NULL;
  898. int error;
  899. dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
  900. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  901. /* Check that we are indeed trying to open this file */
  902. if (!is_atomic_open(dir, nd))
  903. goto no_open;
  904. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  905. res = ERR_PTR(-ENAMETOOLONG);
  906. goto out;
  907. }
  908. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  909. /* Let vfs_create() deal with O_EXCL */
  910. if (nd->intent.open.flags & O_EXCL) {
  911. d_add(dentry, NULL);
  912. goto out;
  913. }
  914. /* Open the file on the server */
  915. lock_kernel();
  916. /* Revalidate parent directory attribute cache */
  917. error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
  918. if (error < 0) {
  919. res = ERR_PTR(error);
  920. unlock_kernel();
  921. goto out;
  922. }
  923. if (nd->intent.open.flags & O_CREAT) {
  924. nfs_begin_data_update(dir);
  925. res = nfs4_atomic_open(dir, dentry, nd);
  926. nfs_end_data_update(dir);
  927. } else
  928. res = nfs4_atomic_open(dir, dentry, nd);
  929. unlock_kernel();
  930. if (IS_ERR(res)) {
  931. error = PTR_ERR(res);
  932. switch (error) {
  933. /* Make a negative dentry */
  934. case -ENOENT:
  935. res = NULL;
  936. goto out;
  937. /* This turned out not to be a regular file */
  938. case -EISDIR:
  939. case -ENOTDIR:
  940. goto no_open;
  941. case -ELOOP:
  942. if (!(nd->intent.open.flags & O_NOFOLLOW))
  943. goto no_open;
  944. /* case -EINVAL: */
  945. default:
  946. goto out;
  947. }
  948. } else if (res != NULL)
  949. dentry = res;
  950. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  951. out:
  952. return res;
  953. no_open:
  954. return nfs_lookup(dir, dentry, nd);
  955. }
  956. static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
  957. {
  958. struct dentry *parent = NULL;
  959. struct inode *inode = dentry->d_inode;
  960. struct inode *dir;
  961. unsigned long verifier;
  962. int openflags, ret = 0;
  963. parent = dget_parent(dentry);
  964. dir = parent->d_inode;
  965. if (!is_atomic_open(dir, nd))
  966. goto no_open;
  967. /* We can't create new files in nfs_open_revalidate(), so we
  968. * optimize away revalidation of negative dentries.
  969. */
  970. if (inode == NULL)
  971. goto out;
  972. /* NFS only supports OPEN on regular files */
  973. if (!S_ISREG(inode->i_mode))
  974. goto no_open;
  975. openflags = nd->intent.open.flags;
  976. /* We cannot do exclusive creation on a positive dentry */
  977. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  978. goto no_open;
  979. /* We can't create new files, or truncate existing ones here */
  980. openflags &= ~(O_CREAT|O_TRUNC);
  981. /*
  982. * Note: we're not holding inode->i_mutex and so may be racing with
  983. * operations that change the directory. We therefore save the
  984. * change attribute *before* we do the RPC call.
  985. */
  986. lock_kernel();
  987. verifier = nfs_save_change_attribute(dir);
  988. ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
  989. if (!ret)
  990. nfs_refresh_verifier(dentry, verifier);
  991. unlock_kernel();
  992. out:
  993. dput(parent);
  994. if (!ret)
  995. d_drop(dentry);
  996. return ret;
  997. no_open:
  998. dput(parent);
  999. if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
  1000. return 1;
  1001. return nfs_lookup_revalidate(dentry, nd);
  1002. }
  1003. #endif /* CONFIG_NFSV4 */
  1004. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
  1005. {
  1006. struct dentry *parent = desc->file->f_path.dentry;
  1007. struct inode *dir = parent->d_inode;
  1008. struct nfs_entry *entry = desc->entry;
  1009. struct dentry *dentry, *alias;
  1010. struct qstr name = {
  1011. .name = entry->name,
  1012. .len = entry->len,
  1013. };
  1014. struct inode *inode;
  1015. switch (name.len) {
  1016. case 2:
  1017. if (name.name[0] == '.' && name.name[1] == '.')
  1018. return dget_parent(parent);
  1019. break;
  1020. case 1:
  1021. if (name.name[0] == '.')
  1022. return dget(parent);
  1023. }
  1024. name.hash = full_name_hash(name.name, name.len);
  1025. dentry = d_lookup(parent, &name);
  1026. if (dentry != NULL) {
  1027. /* Is this a positive dentry that matches the readdir info? */
  1028. if (dentry->d_inode != NULL &&
  1029. (NFS_FILEID(dentry->d_inode) == entry->ino ||
  1030. d_mountpoint(dentry))) {
  1031. if (!desc->plus || entry->fh->size == 0)
  1032. return dentry;
  1033. if (nfs_compare_fh(NFS_FH(dentry->d_inode),
  1034. entry->fh) == 0)
  1035. goto out_renew;
  1036. }
  1037. /* No, so d_drop to allow one to be created */
  1038. d_drop(dentry);
  1039. dput(dentry);
  1040. }
  1041. if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
  1042. return NULL;
  1043. if (name.len > NFS_SERVER(dir)->namelen)
  1044. return NULL;
  1045. /* Note: caller is already holding the dir->i_mutex! */
  1046. dentry = d_alloc(parent, &name);
  1047. if (dentry == NULL)
  1048. return NULL;
  1049. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  1050. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  1051. if (IS_ERR(inode)) {
  1052. dput(dentry);
  1053. return NULL;
  1054. }
  1055. alias = d_materialise_unique(dentry, inode);
  1056. if (alias != NULL) {
  1057. dput(dentry);
  1058. if (IS_ERR(alias))
  1059. return NULL;
  1060. dentry = alias;
  1061. }
  1062. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1063. return dentry;
  1064. out_renew:
  1065. nfs_refresh_verifier(dentry, nfs_save_change_attribute(dir));
  1066. return dentry;
  1067. }
  1068. /*
  1069. * Code common to create, mkdir, and mknod.
  1070. */
  1071. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1072. struct nfs_fattr *fattr)
  1073. {
  1074. struct inode *inode;
  1075. int error = -EACCES;
  1076. /* We may have been initialized further down */
  1077. if (dentry->d_inode)
  1078. return 0;
  1079. if (fhandle->size == 0) {
  1080. struct inode *dir = dentry->d_parent->d_inode;
  1081. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1082. if (error)
  1083. return error;
  1084. }
  1085. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1086. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1087. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
  1088. if (error < 0)
  1089. return error;
  1090. }
  1091. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1092. error = PTR_ERR(inode);
  1093. if (IS_ERR(inode))
  1094. return error;
  1095. d_instantiate(dentry, inode);
  1096. if (d_unhashed(dentry))
  1097. d_rehash(dentry);
  1098. return 0;
  1099. }
  1100. /*
  1101. * Following a failed create operation, we drop the dentry rather
  1102. * than retain a negative dentry. This avoids a problem in the event
  1103. * that the operation succeeded on the server, but an error in the
  1104. * reply path made it appear to have failed.
  1105. */
  1106. static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1107. struct nameidata *nd)
  1108. {
  1109. struct iattr attr;
  1110. int error;
  1111. int open_flags = 0;
  1112. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1113. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1114. attr.ia_mode = mode;
  1115. attr.ia_valid = ATTR_MODE;
  1116. if ((nd->flags & LOOKUP_CREATE) != 0)
  1117. open_flags = nd->intent.open.flags;
  1118. lock_kernel();
  1119. nfs_begin_data_update(dir);
  1120. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
  1121. nfs_end_data_update(dir);
  1122. if (error != 0)
  1123. goto out_err;
  1124. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1125. unlock_kernel();
  1126. return 0;
  1127. out_err:
  1128. unlock_kernel();
  1129. d_drop(dentry);
  1130. return error;
  1131. }
  1132. /*
  1133. * See comments for nfs_proc_create regarding failed operations.
  1134. */
  1135. static int
  1136. nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1137. {
  1138. struct iattr attr;
  1139. int status;
  1140. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1141. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1142. if (!new_valid_dev(rdev))
  1143. return -EINVAL;
  1144. attr.ia_mode = mode;
  1145. attr.ia_valid = ATTR_MODE;
  1146. lock_kernel();
  1147. nfs_begin_data_update(dir);
  1148. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1149. nfs_end_data_update(dir);
  1150. if (status != 0)
  1151. goto out_err;
  1152. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1153. unlock_kernel();
  1154. return 0;
  1155. out_err:
  1156. unlock_kernel();
  1157. d_drop(dentry);
  1158. return status;
  1159. }
  1160. /*
  1161. * See comments for nfs_proc_create regarding failed operations.
  1162. */
  1163. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1164. {
  1165. struct iattr attr;
  1166. int error;
  1167. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1168. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1169. attr.ia_valid = ATTR_MODE;
  1170. attr.ia_mode = mode | S_IFDIR;
  1171. lock_kernel();
  1172. nfs_begin_data_update(dir);
  1173. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1174. nfs_end_data_update(dir);
  1175. if (error != 0)
  1176. goto out_err;
  1177. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1178. unlock_kernel();
  1179. return 0;
  1180. out_err:
  1181. d_drop(dentry);
  1182. unlock_kernel();
  1183. return error;
  1184. }
  1185. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1186. {
  1187. int error;
  1188. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1189. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1190. lock_kernel();
  1191. nfs_begin_data_update(dir);
  1192. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1193. /* Ensure the VFS deletes this inode */
  1194. if (error == 0 && dentry->d_inode != NULL)
  1195. clear_nlink(dentry->d_inode);
  1196. nfs_end_data_update(dir);
  1197. unlock_kernel();
  1198. return error;
  1199. }
  1200. static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
  1201. {
  1202. static unsigned int sillycounter;
  1203. const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2;
  1204. const int countersize = sizeof(sillycounter)*2;
  1205. const int slen = sizeof(".nfs")+fileidsize+countersize-1;
  1206. char silly[slen+1];
  1207. struct qstr qsilly;
  1208. struct dentry *sdentry;
  1209. int error = -EIO;
  1210. dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
  1211. dentry->d_parent->d_name.name, dentry->d_name.name,
  1212. atomic_read(&dentry->d_count));
  1213. nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
  1214. /*
  1215. * We don't allow a dentry to be silly-renamed twice.
  1216. */
  1217. error = -EBUSY;
  1218. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1219. goto out;
  1220. sprintf(silly, ".nfs%*.*Lx",
  1221. fileidsize, fileidsize,
  1222. (unsigned long long)NFS_FILEID(dentry->d_inode));
  1223. /* Return delegation in anticipation of the rename */
  1224. nfs_inode_return_delegation(dentry->d_inode);
  1225. sdentry = NULL;
  1226. do {
  1227. char *suffix = silly + slen - countersize;
  1228. dput(sdentry);
  1229. sillycounter++;
  1230. sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
  1231. dfprintk(VFS, "NFS: trying to rename %s to %s\n",
  1232. dentry->d_name.name, silly);
  1233. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  1234. /*
  1235. * N.B. Better to return EBUSY here ... it could be
  1236. * dangerous to delete the file while it's in use.
  1237. */
  1238. if (IS_ERR(sdentry))
  1239. goto out;
  1240. } while(sdentry->d_inode != NULL); /* need negative lookup */
  1241. qsilly.name = silly;
  1242. qsilly.len = strlen(silly);
  1243. nfs_begin_data_update(dir);
  1244. if (dentry->d_inode) {
  1245. nfs_begin_data_update(dentry->d_inode);
  1246. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1247. dir, &qsilly);
  1248. nfs_mark_for_revalidate(dentry->d_inode);
  1249. nfs_end_data_update(dentry->d_inode);
  1250. } else
  1251. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1252. dir, &qsilly);
  1253. nfs_end_data_update(dir);
  1254. if (!error) {
  1255. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1256. d_move(dentry, sdentry);
  1257. error = nfs_async_unlink(dir, dentry);
  1258. /* If we return 0 we don't unlink */
  1259. }
  1260. dput(sdentry);
  1261. out:
  1262. return error;
  1263. }
  1264. /*
  1265. * Remove a file after making sure there are no pending writes,
  1266. * and after checking that the file has only one user.
  1267. *
  1268. * We invalidate the attribute cache and free the inode prior to the operation
  1269. * to avoid possible races if the server reuses the inode.
  1270. */
  1271. static int nfs_safe_remove(struct dentry *dentry)
  1272. {
  1273. struct inode *dir = dentry->d_parent->d_inode;
  1274. struct inode *inode = dentry->d_inode;
  1275. int error = -EBUSY;
  1276. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1277. dentry->d_parent->d_name.name, dentry->d_name.name);
  1278. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1279. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1280. error = 0;
  1281. goto out;
  1282. }
  1283. nfs_begin_data_update(dir);
  1284. if (inode != NULL) {
  1285. nfs_inode_return_delegation(inode);
  1286. nfs_begin_data_update(inode);
  1287. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1288. /* The VFS may want to delete this inode */
  1289. if (error == 0)
  1290. drop_nlink(inode);
  1291. nfs_mark_for_revalidate(inode);
  1292. nfs_end_data_update(inode);
  1293. } else
  1294. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1295. nfs_end_data_update(dir);
  1296. out:
  1297. return error;
  1298. }
  1299. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1300. * belongs to an active ".nfs..." file and we return -EBUSY.
  1301. *
  1302. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1303. */
  1304. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1305. {
  1306. int error;
  1307. int need_rehash = 0;
  1308. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1309. dir->i_ino, dentry->d_name.name);
  1310. lock_kernel();
  1311. spin_lock(&dcache_lock);
  1312. spin_lock(&dentry->d_lock);
  1313. if (atomic_read(&dentry->d_count) > 1) {
  1314. spin_unlock(&dentry->d_lock);
  1315. spin_unlock(&dcache_lock);
  1316. /* Start asynchronous writeout of the inode */
  1317. write_inode_now(dentry->d_inode, 0);
  1318. error = nfs_sillyrename(dir, dentry);
  1319. unlock_kernel();
  1320. return error;
  1321. }
  1322. if (!d_unhashed(dentry)) {
  1323. __d_drop(dentry);
  1324. need_rehash = 1;
  1325. }
  1326. spin_unlock(&dentry->d_lock);
  1327. spin_unlock(&dcache_lock);
  1328. error = nfs_safe_remove(dentry);
  1329. if (!error) {
  1330. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1331. } else if (need_rehash)
  1332. d_rehash(dentry);
  1333. unlock_kernel();
  1334. return error;
  1335. }
  1336. /*
  1337. * To create a symbolic link, most file systems instantiate a new inode,
  1338. * add a page to it containing the path, then write it out to the disk
  1339. * using prepare_write/commit_write.
  1340. *
  1341. * Unfortunately the NFS client can't create the in-core inode first
  1342. * because it needs a file handle to create an in-core inode (see
  1343. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1344. * symlink request has completed on the server.
  1345. *
  1346. * So instead we allocate a raw page, copy the symname into it, then do
  1347. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1348. * now have a new file handle and can instantiate an in-core NFS inode
  1349. * and move the raw page into its mapping.
  1350. */
  1351. static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1352. {
  1353. struct pagevec lru_pvec;
  1354. struct page *page;
  1355. char *kaddr;
  1356. struct iattr attr;
  1357. unsigned int pathlen = strlen(symname);
  1358. int error;
  1359. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1360. dir->i_ino, dentry->d_name.name, symname);
  1361. if (pathlen > PAGE_SIZE)
  1362. return -ENAMETOOLONG;
  1363. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1364. attr.ia_valid = ATTR_MODE;
  1365. lock_kernel();
  1366. page = alloc_page(GFP_HIGHUSER);
  1367. if (!page) {
  1368. unlock_kernel();
  1369. return -ENOMEM;
  1370. }
  1371. kaddr = kmap_atomic(page, KM_USER0);
  1372. memcpy(kaddr, symname, pathlen);
  1373. if (pathlen < PAGE_SIZE)
  1374. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1375. kunmap_atomic(kaddr, KM_USER0);
  1376. nfs_begin_data_update(dir);
  1377. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1378. nfs_end_data_update(dir);
  1379. if (error != 0) {
  1380. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1381. dir->i_sb->s_id, dir->i_ino,
  1382. dentry->d_name.name, symname, error);
  1383. d_drop(dentry);
  1384. __free_page(page);
  1385. unlock_kernel();
  1386. return error;
  1387. }
  1388. /*
  1389. * No big deal if we can't add this page to the page cache here.
  1390. * READLINK will get the missing page from the server if needed.
  1391. */
  1392. pagevec_init(&lru_pvec, 0);
  1393. if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
  1394. GFP_KERNEL)) {
  1395. pagevec_add(&lru_pvec, page);
  1396. pagevec_lru_add(&lru_pvec);
  1397. SetPageUptodate(page);
  1398. unlock_page(page);
  1399. } else
  1400. __free_page(page);
  1401. unlock_kernel();
  1402. return 0;
  1403. }
  1404. static int
  1405. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1406. {
  1407. struct inode *inode = old_dentry->d_inode;
  1408. int error;
  1409. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1410. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1411. dentry->d_parent->d_name.name, dentry->d_name.name);
  1412. lock_kernel();
  1413. nfs_begin_data_update(dir);
  1414. nfs_begin_data_update(inode);
  1415. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1416. if (error == 0) {
  1417. atomic_inc(&inode->i_count);
  1418. d_instantiate(dentry, inode);
  1419. }
  1420. nfs_end_data_update(inode);
  1421. nfs_end_data_update(dir);
  1422. unlock_kernel();
  1423. return error;
  1424. }
  1425. /*
  1426. * RENAME
  1427. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1428. * different file handle for the same inode after a rename (e.g. when
  1429. * moving to a different directory). A fail-safe method to do so would
  1430. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1431. * rename the old file using the sillyrename stuff. This way, the original
  1432. * file in old_dir will go away when the last process iput()s the inode.
  1433. *
  1434. * FIXED.
  1435. *
  1436. * It actually works quite well. One needs to have the possibility for
  1437. * at least one ".nfs..." file in each directory the file ever gets
  1438. * moved or linked to which happens automagically with the new
  1439. * implementation that only depends on the dcache stuff instead of
  1440. * using the inode layer
  1441. *
  1442. * Unfortunately, things are a little more complicated than indicated
  1443. * above. For a cross-directory move, we want to make sure we can get
  1444. * rid of the old inode after the operation. This means there must be
  1445. * no pending writes (if it's a file), and the use count must be 1.
  1446. * If these conditions are met, we can drop the dentries before doing
  1447. * the rename.
  1448. */
  1449. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1450. struct inode *new_dir, struct dentry *new_dentry)
  1451. {
  1452. struct inode *old_inode = old_dentry->d_inode;
  1453. struct inode *new_inode = new_dentry->d_inode;
  1454. struct dentry *dentry = NULL, *rehash = NULL;
  1455. int error = -EBUSY;
  1456. /*
  1457. * To prevent any new references to the target during the rename,
  1458. * we unhash the dentry and free the inode in advance.
  1459. */
  1460. lock_kernel();
  1461. if (!d_unhashed(new_dentry)) {
  1462. d_drop(new_dentry);
  1463. rehash = new_dentry;
  1464. }
  1465. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1466. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1467. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1468. atomic_read(&new_dentry->d_count));
  1469. /*
  1470. * First check whether the target is busy ... we can't
  1471. * safely do _any_ rename if the target is in use.
  1472. *
  1473. * For files, make a copy of the dentry and then do a
  1474. * silly-rename. If the silly-rename succeeds, the
  1475. * copied dentry is hashed and becomes the new target.
  1476. */
  1477. if (!new_inode)
  1478. goto go_ahead;
  1479. if (S_ISDIR(new_inode->i_mode)) {
  1480. error = -EISDIR;
  1481. if (!S_ISDIR(old_inode->i_mode))
  1482. goto out;
  1483. } else if (atomic_read(&new_dentry->d_count) > 2) {
  1484. int err;
  1485. /* copy the target dentry's name */
  1486. dentry = d_alloc(new_dentry->d_parent,
  1487. &new_dentry->d_name);
  1488. if (!dentry)
  1489. goto out;
  1490. /* silly-rename the existing target ... */
  1491. err = nfs_sillyrename(new_dir, new_dentry);
  1492. if (!err) {
  1493. new_dentry = rehash = dentry;
  1494. new_inode = NULL;
  1495. /* instantiate the replacement target */
  1496. d_instantiate(new_dentry, NULL);
  1497. } else if (atomic_read(&new_dentry->d_count) > 1)
  1498. /* dentry still busy? */
  1499. goto out;
  1500. } else
  1501. drop_nlink(new_inode);
  1502. go_ahead:
  1503. /*
  1504. * ... prune child dentries and writebacks if needed.
  1505. */
  1506. if (atomic_read(&old_dentry->d_count) > 1) {
  1507. if (S_ISREG(old_inode->i_mode))
  1508. nfs_wb_all(old_inode);
  1509. shrink_dcache_parent(old_dentry);
  1510. }
  1511. nfs_inode_return_delegation(old_inode);
  1512. if (new_inode != NULL) {
  1513. nfs_inode_return_delegation(new_inode);
  1514. d_delete(new_dentry);
  1515. }
  1516. nfs_begin_data_update(old_dir);
  1517. nfs_begin_data_update(new_dir);
  1518. nfs_begin_data_update(old_inode);
  1519. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1520. new_dir, &new_dentry->d_name);
  1521. nfs_mark_for_revalidate(old_inode);
  1522. nfs_end_data_update(old_inode);
  1523. nfs_end_data_update(new_dir);
  1524. nfs_end_data_update(old_dir);
  1525. out:
  1526. if (rehash)
  1527. d_rehash(rehash);
  1528. if (!error) {
  1529. d_move(old_dentry, new_dentry);
  1530. nfs_refresh_verifier(new_dentry, nfs_save_change_attribute(new_dir));
  1531. }
  1532. /* new dentry created? */
  1533. if (dentry)
  1534. dput(dentry);
  1535. unlock_kernel();
  1536. return error;
  1537. }
  1538. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1539. static LIST_HEAD(nfs_access_lru_list);
  1540. static atomic_long_t nfs_access_nr_entries;
  1541. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1542. {
  1543. put_rpccred(entry->cred);
  1544. kfree(entry);
  1545. smp_mb__before_atomic_dec();
  1546. atomic_long_dec(&nfs_access_nr_entries);
  1547. smp_mb__after_atomic_dec();
  1548. }
  1549. int nfs_access_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
  1550. {
  1551. LIST_HEAD(head);
  1552. struct nfs_inode *nfsi;
  1553. struct nfs_access_entry *cache;
  1554. restart:
  1555. spin_lock(&nfs_access_lru_lock);
  1556. list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
  1557. struct inode *inode;
  1558. if (nr_to_scan-- == 0)
  1559. break;
  1560. inode = igrab(&nfsi->vfs_inode);
  1561. if (inode == NULL)
  1562. continue;
  1563. spin_lock(&inode->i_lock);
  1564. if (list_empty(&nfsi->access_cache_entry_lru))
  1565. goto remove_lru_entry;
  1566. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1567. struct nfs_access_entry, lru);
  1568. list_move(&cache->lru, &head);
  1569. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1570. if (!list_empty(&nfsi->access_cache_entry_lru))
  1571. list_move_tail(&nfsi->access_cache_inode_lru,
  1572. &nfs_access_lru_list);
  1573. else {
  1574. remove_lru_entry:
  1575. list_del_init(&nfsi->access_cache_inode_lru);
  1576. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1577. }
  1578. spin_unlock(&inode->i_lock);
  1579. spin_unlock(&nfs_access_lru_lock);
  1580. iput(inode);
  1581. goto restart;
  1582. }
  1583. spin_unlock(&nfs_access_lru_lock);
  1584. while (!list_empty(&head)) {
  1585. cache = list_entry(head.next, struct nfs_access_entry, lru);
  1586. list_del(&cache->lru);
  1587. nfs_access_free_entry(cache);
  1588. }
  1589. return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
  1590. }
  1591. static void __nfs_access_zap_cache(struct inode *inode)
  1592. {
  1593. struct nfs_inode *nfsi = NFS_I(inode);
  1594. struct rb_root *root_node = &nfsi->access_cache;
  1595. struct rb_node *n, *dispose = NULL;
  1596. struct nfs_access_entry *entry;
  1597. /* Unhook entries from the cache */
  1598. while ((n = rb_first(root_node)) != NULL) {
  1599. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1600. rb_erase(n, root_node);
  1601. list_del(&entry->lru);
  1602. n->rb_left = dispose;
  1603. dispose = n;
  1604. }
  1605. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1606. spin_unlock(&inode->i_lock);
  1607. /* Now kill them all! */
  1608. while (dispose != NULL) {
  1609. n = dispose;
  1610. dispose = n->rb_left;
  1611. nfs_access_free_entry(rb_entry(n, struct nfs_access_entry, rb_node));
  1612. }
  1613. }
  1614. void nfs_access_zap_cache(struct inode *inode)
  1615. {
  1616. /* Remove from global LRU init */
  1617. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
  1618. spin_lock(&nfs_access_lru_lock);
  1619. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1620. spin_unlock(&nfs_access_lru_lock);
  1621. }
  1622. spin_lock(&inode->i_lock);
  1623. /* This will release the spinlock */
  1624. __nfs_access_zap_cache(inode);
  1625. }
  1626. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1627. {
  1628. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1629. struct nfs_access_entry *entry;
  1630. while (n != NULL) {
  1631. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1632. if (cred < entry->cred)
  1633. n = n->rb_left;
  1634. else if (cred > entry->cred)
  1635. n = n->rb_right;
  1636. else
  1637. return entry;
  1638. }
  1639. return NULL;
  1640. }
  1641. int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1642. {
  1643. struct nfs_inode *nfsi = NFS_I(inode);
  1644. struct nfs_access_entry *cache;
  1645. int err = -ENOENT;
  1646. spin_lock(&inode->i_lock);
  1647. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  1648. goto out_zap;
  1649. cache = nfs_access_search_rbtree(inode, cred);
  1650. if (cache == NULL)
  1651. goto out;
  1652. if (!time_in_range(jiffies, cache->jiffies, cache->jiffies + NFS_ATTRTIMEO(inode)))
  1653. goto out_stale;
  1654. res->jiffies = cache->jiffies;
  1655. res->cred = cache->cred;
  1656. res->mask = cache->mask;
  1657. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1658. err = 0;
  1659. out:
  1660. spin_unlock(&inode->i_lock);
  1661. return err;
  1662. out_stale:
  1663. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1664. list_del(&cache->lru);
  1665. spin_unlock(&inode->i_lock);
  1666. nfs_access_free_entry(cache);
  1667. return -ENOENT;
  1668. out_zap:
  1669. /* This will release the spinlock */
  1670. __nfs_access_zap_cache(inode);
  1671. return -ENOENT;
  1672. }
  1673. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1674. {
  1675. struct nfs_inode *nfsi = NFS_I(inode);
  1676. struct rb_root *root_node = &nfsi->access_cache;
  1677. struct rb_node **p = &root_node->rb_node;
  1678. struct rb_node *parent = NULL;
  1679. struct nfs_access_entry *entry;
  1680. spin_lock(&inode->i_lock);
  1681. while (*p != NULL) {
  1682. parent = *p;
  1683. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1684. if (set->cred < entry->cred)
  1685. p = &parent->rb_left;
  1686. else if (set->cred > entry->cred)
  1687. p = &parent->rb_right;
  1688. else
  1689. goto found;
  1690. }
  1691. rb_link_node(&set->rb_node, parent, p);
  1692. rb_insert_color(&set->rb_node, root_node);
  1693. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1694. spin_unlock(&inode->i_lock);
  1695. return;
  1696. found:
  1697. rb_replace_node(parent, &set->rb_node, root_node);
  1698. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1699. list_del(&entry->lru);
  1700. spin_unlock(&inode->i_lock);
  1701. nfs_access_free_entry(entry);
  1702. }
  1703. void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1704. {
  1705. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1706. if (cache == NULL)
  1707. return;
  1708. RB_CLEAR_NODE(&cache->rb_node);
  1709. cache->jiffies = set->jiffies;
  1710. cache->cred = get_rpccred(set->cred);
  1711. cache->mask = set->mask;
  1712. nfs_access_add_rbtree(inode, cache);
  1713. /* Update accounting */
  1714. smp_mb__before_atomic_inc();
  1715. atomic_long_inc(&nfs_access_nr_entries);
  1716. smp_mb__after_atomic_inc();
  1717. /* Add inode to global LRU list */
  1718. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
  1719. spin_lock(&nfs_access_lru_lock);
  1720. list_add_tail(&NFS_I(inode)->access_cache_inode_lru, &nfs_access_lru_list);
  1721. spin_unlock(&nfs_access_lru_lock);
  1722. }
  1723. }
  1724. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1725. {
  1726. struct nfs_access_entry cache;
  1727. int status;
  1728. status = nfs_access_get_cached(inode, cred, &cache);
  1729. if (status == 0)
  1730. goto out;
  1731. /* Be clever: ask server to check for all possible rights */
  1732. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1733. cache.cred = cred;
  1734. cache.jiffies = jiffies;
  1735. status = NFS_PROTO(inode)->access(inode, &cache);
  1736. if (status != 0)
  1737. return status;
  1738. nfs_access_add_cache(inode, &cache);
  1739. out:
  1740. if ((cache.mask & mask) == mask)
  1741. return 0;
  1742. return -EACCES;
  1743. }
  1744. int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  1745. {
  1746. struct rpc_cred *cred;
  1747. int res = 0;
  1748. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  1749. if (mask == 0)
  1750. goto out;
  1751. /* Is this sys_access() ? */
  1752. if (nd != NULL && (nd->flags & LOOKUP_ACCESS))
  1753. goto force_lookup;
  1754. switch (inode->i_mode & S_IFMT) {
  1755. case S_IFLNK:
  1756. goto out;
  1757. case S_IFREG:
  1758. /* NFSv4 has atomic_open... */
  1759. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  1760. && nd != NULL
  1761. && (nd->flags & LOOKUP_OPEN))
  1762. goto out;
  1763. break;
  1764. case S_IFDIR:
  1765. /*
  1766. * Optimize away all write operations, since the server
  1767. * will check permissions when we perform the op.
  1768. */
  1769. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  1770. goto out;
  1771. }
  1772. force_lookup:
  1773. lock_kernel();
  1774. if (!NFS_PROTO(inode)->access)
  1775. goto out_notsup;
  1776. cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
  1777. if (!IS_ERR(cred)) {
  1778. res = nfs_do_access(inode, cred, mask);
  1779. put_rpccred(cred);
  1780. } else
  1781. res = PTR_ERR(cred);
  1782. unlock_kernel();
  1783. out:
  1784. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  1785. inode->i_sb->s_id, inode->i_ino, mask, res);
  1786. return res;
  1787. out_notsup:
  1788. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1789. if (res == 0)
  1790. res = generic_permission(inode, mask, NULL);
  1791. unlock_kernel();
  1792. goto out;
  1793. }
  1794. /*
  1795. * Local variables:
  1796. * version-control: t
  1797. * kept-new-versions: 5
  1798. * End:
  1799. */