dir.c 44 KB

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