dir.c 53 KB

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