dir.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342
  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/pagevec.h>
  32. #include <linux/namei.h>
  33. #include <linux/mount.h>
  34. #include <linux/sched.h>
  35. #include <linux/kmemleak.h>
  36. #include <linux/xattr.h>
  37. #include "delegation.h"
  38. #include "iostat.h"
  39. #include "internal.h"
  40. #include "fscache.h"
  41. /* #define NFS_DEBUG_VERBOSE 1 */
  42. static int nfs_opendir(struct inode *, struct file *);
  43. static int nfs_closedir(struct inode *, struct file *);
  44. static int nfs_readdir(struct file *, void *, filldir_t);
  45. static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
  46. static int nfs_create(struct inode *, struct dentry *, umode_t, struct nameidata *);
  47. static int nfs_mkdir(struct inode *, struct dentry *, umode_t);
  48. static int nfs_rmdir(struct inode *, struct dentry *);
  49. static int nfs_unlink(struct inode *, struct dentry *);
  50. static int nfs_symlink(struct inode *, struct dentry *, const char *);
  51. static int nfs_link(struct dentry *, struct inode *, struct dentry *);
  52. static int nfs_mknod(struct inode *, struct dentry *, umode_t, dev_t);
  53. static int nfs_rename(struct inode *, struct dentry *,
  54. struct inode *, struct dentry *);
  55. static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
  56. static loff_t nfs_llseek_dir(struct file *, loff_t, int);
  57. static void nfs_readdir_clear_array(struct page*);
  58. const struct file_operations nfs_dir_operations = {
  59. .llseek = nfs_llseek_dir,
  60. .read = generic_read_dir,
  61. .readdir = nfs_readdir,
  62. .open = nfs_opendir,
  63. .release = nfs_closedir,
  64. .fsync = nfs_fsync_dir,
  65. };
  66. const struct inode_operations nfs_dir_inode_operations = {
  67. .create = nfs_create,
  68. .lookup = nfs_lookup,
  69. .link = nfs_link,
  70. .unlink = nfs_unlink,
  71. .symlink = nfs_symlink,
  72. .mkdir = nfs_mkdir,
  73. .rmdir = nfs_rmdir,
  74. .mknod = nfs_mknod,
  75. .rename = nfs_rename,
  76. .permission = nfs_permission,
  77. .getattr = nfs_getattr,
  78. .setattr = nfs_setattr,
  79. };
  80. const struct address_space_operations nfs_dir_aops = {
  81. .freepage = nfs_readdir_clear_array,
  82. };
  83. #ifdef CONFIG_NFS_V3
  84. const struct inode_operations nfs3_dir_inode_operations = {
  85. .create = nfs_create,
  86. .lookup = nfs_lookup,
  87. .link = nfs_link,
  88. .unlink = nfs_unlink,
  89. .symlink = nfs_symlink,
  90. .mkdir = nfs_mkdir,
  91. .rmdir = nfs_rmdir,
  92. .mknod = nfs_mknod,
  93. .rename = nfs_rename,
  94. .permission = nfs_permission,
  95. .getattr = nfs_getattr,
  96. .setattr = nfs_setattr,
  97. .listxattr = nfs3_listxattr,
  98. .getxattr = nfs3_getxattr,
  99. .setxattr = nfs3_setxattr,
  100. .removexattr = nfs3_removexattr,
  101. };
  102. #endif /* CONFIG_NFS_V3 */
  103. #ifdef CONFIG_NFS_V4
  104. static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
  105. static int nfs_open_create(struct inode *dir, struct dentry *dentry, umode_t mode, struct nameidata *nd);
  106. const struct inode_operations nfs4_dir_inode_operations = {
  107. .create = nfs_open_create,
  108. .lookup = nfs_atomic_lookup,
  109. .link = nfs_link,
  110. .unlink = nfs_unlink,
  111. .symlink = nfs_symlink,
  112. .mkdir = nfs_mkdir,
  113. .rmdir = nfs_rmdir,
  114. .mknod = nfs_mknod,
  115. .rename = nfs_rename,
  116. .permission = nfs_permission,
  117. .getattr = nfs_getattr,
  118. .setattr = nfs_setattr,
  119. .getxattr = generic_getxattr,
  120. .setxattr = generic_setxattr,
  121. .listxattr = generic_listxattr,
  122. .removexattr = generic_removexattr,
  123. };
  124. #endif /* CONFIG_NFS_V4 */
  125. static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
  126. {
  127. struct nfs_open_dir_context *ctx;
  128. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  129. if (ctx != NULL) {
  130. ctx->duped = 0;
  131. ctx->attr_gencount = NFS_I(dir)->attr_gencount;
  132. ctx->dir_cookie = 0;
  133. ctx->dup_cookie = 0;
  134. ctx->cred = get_rpccred(cred);
  135. return ctx;
  136. }
  137. return ERR_PTR(-ENOMEM);
  138. }
  139. static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
  140. {
  141. put_rpccred(ctx->cred);
  142. kfree(ctx);
  143. }
  144. /*
  145. * Open file
  146. */
  147. static int
  148. nfs_opendir(struct inode *inode, struct file *filp)
  149. {
  150. int res = 0;
  151. struct nfs_open_dir_context *ctx;
  152. struct rpc_cred *cred;
  153. dfprintk(FILE, "NFS: open dir(%s/%s)\n",
  154. filp->f_path.dentry->d_parent->d_name.name,
  155. filp->f_path.dentry->d_name.name);
  156. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  157. cred = rpc_lookup_cred();
  158. if (IS_ERR(cred))
  159. return PTR_ERR(cred);
  160. ctx = alloc_nfs_open_dir_context(inode, cred);
  161. if (IS_ERR(ctx)) {
  162. res = PTR_ERR(ctx);
  163. goto out;
  164. }
  165. filp->private_data = ctx;
  166. if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
  167. /* This is a mountpoint, so d_revalidate will never
  168. * have been called, so we need to refresh the
  169. * inode (for close-open consistency) ourselves.
  170. */
  171. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  172. }
  173. out:
  174. put_rpccred(cred);
  175. return res;
  176. }
  177. static int
  178. nfs_closedir(struct inode *inode, struct file *filp)
  179. {
  180. put_nfs_open_dir_context(filp->private_data);
  181. return 0;
  182. }
  183. struct nfs_cache_array_entry {
  184. u64 cookie;
  185. u64 ino;
  186. struct qstr string;
  187. unsigned char d_type;
  188. };
  189. struct nfs_cache_array {
  190. int size;
  191. int eof_index;
  192. u64 last_cookie;
  193. struct nfs_cache_array_entry array[0];
  194. };
  195. typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
  196. typedef struct {
  197. struct file *file;
  198. struct page *page;
  199. unsigned long page_index;
  200. u64 *dir_cookie;
  201. u64 last_cookie;
  202. loff_t current_index;
  203. decode_dirent_t decode;
  204. unsigned long timestamp;
  205. unsigned long gencount;
  206. unsigned int cache_entry_index;
  207. unsigned int plus:1;
  208. unsigned int eof:1;
  209. } nfs_readdir_descriptor_t;
  210. /*
  211. * The caller is responsible for calling nfs_readdir_release_array(page)
  212. */
  213. static
  214. struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
  215. {
  216. void *ptr;
  217. if (page == NULL)
  218. return ERR_PTR(-EIO);
  219. ptr = kmap(page);
  220. if (ptr == NULL)
  221. return ERR_PTR(-ENOMEM);
  222. return ptr;
  223. }
  224. static
  225. void nfs_readdir_release_array(struct page *page)
  226. {
  227. kunmap(page);
  228. }
  229. /*
  230. * we are freeing strings created by nfs_add_to_readdir_array()
  231. */
  232. static
  233. void nfs_readdir_clear_array(struct page *page)
  234. {
  235. struct nfs_cache_array *array;
  236. int i;
  237. array = kmap_atomic(page);
  238. for (i = 0; i < array->size; i++)
  239. kfree(array->array[i].string.name);
  240. kunmap_atomic(array);
  241. }
  242. /*
  243. * the caller is responsible for freeing qstr.name
  244. * when called by nfs_readdir_add_to_array, the strings will be freed in
  245. * nfs_clear_readdir_array()
  246. */
  247. static
  248. int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
  249. {
  250. string->len = len;
  251. string->name = kmemdup(name, len, GFP_KERNEL);
  252. if (string->name == NULL)
  253. return -ENOMEM;
  254. /*
  255. * Avoid a kmemleak false positive. The pointer to the name is stored
  256. * in a page cache page which kmemleak does not scan.
  257. */
  258. kmemleak_not_leak(string->name);
  259. string->hash = full_name_hash(name, len);
  260. return 0;
  261. }
  262. static
  263. int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
  264. {
  265. struct nfs_cache_array *array = nfs_readdir_get_array(page);
  266. struct nfs_cache_array_entry *cache_entry;
  267. int ret;
  268. if (IS_ERR(array))
  269. return PTR_ERR(array);
  270. cache_entry = &array->array[array->size];
  271. /* Check that this entry lies within the page bounds */
  272. ret = -ENOSPC;
  273. if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
  274. goto out;
  275. cache_entry->cookie = entry->prev_cookie;
  276. cache_entry->ino = entry->ino;
  277. cache_entry->d_type = entry->d_type;
  278. ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
  279. if (ret)
  280. goto out;
  281. array->last_cookie = entry->cookie;
  282. array->size++;
  283. if (entry->eof != 0)
  284. array->eof_index = array->size;
  285. out:
  286. nfs_readdir_release_array(page);
  287. return ret;
  288. }
  289. static
  290. int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
  291. {
  292. loff_t diff = desc->file->f_pos - desc->current_index;
  293. unsigned int index;
  294. if (diff < 0)
  295. goto out_eof;
  296. if (diff >= array->size) {
  297. if (array->eof_index >= 0)
  298. goto out_eof;
  299. return -EAGAIN;
  300. }
  301. index = (unsigned int)diff;
  302. *desc->dir_cookie = array->array[index].cookie;
  303. desc->cache_entry_index = index;
  304. return 0;
  305. out_eof:
  306. desc->eof = 1;
  307. return -EBADCOOKIE;
  308. }
  309. static
  310. int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
  311. {
  312. int i;
  313. loff_t new_pos;
  314. int status = -EAGAIN;
  315. for (i = 0; i < array->size; i++) {
  316. if (array->array[i].cookie == *desc->dir_cookie) {
  317. struct nfs_inode *nfsi = NFS_I(desc->file->f_path.dentry->d_inode);
  318. struct nfs_open_dir_context *ctx = desc->file->private_data;
  319. new_pos = desc->current_index + i;
  320. if (ctx->attr_gencount != nfsi->attr_gencount
  321. || (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))) {
  322. ctx->duped = 0;
  323. ctx->attr_gencount = nfsi->attr_gencount;
  324. } else if (new_pos < desc->file->f_pos) {
  325. if (ctx->duped > 0
  326. && ctx->dup_cookie == *desc->dir_cookie) {
  327. if (printk_ratelimit()) {
  328. pr_notice("NFS: directory %s/%s contains a readdir loop."
  329. "Please contact your server vendor. "
  330. "The file: %s has duplicate cookie %llu\n",
  331. desc->file->f_dentry->d_parent->d_name.name,
  332. desc->file->f_dentry->d_name.name,
  333. array->array[i].string.name,
  334. *desc->dir_cookie);
  335. }
  336. status = -ELOOP;
  337. goto out;
  338. }
  339. ctx->dup_cookie = *desc->dir_cookie;
  340. ctx->duped = -1;
  341. }
  342. desc->file->f_pos = new_pos;
  343. desc->cache_entry_index = i;
  344. return 0;
  345. }
  346. }
  347. if (array->eof_index >= 0) {
  348. status = -EBADCOOKIE;
  349. if (*desc->dir_cookie == array->last_cookie)
  350. desc->eof = 1;
  351. }
  352. out:
  353. return status;
  354. }
  355. static
  356. int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
  357. {
  358. struct nfs_cache_array *array;
  359. int status;
  360. array = nfs_readdir_get_array(desc->page);
  361. if (IS_ERR(array)) {
  362. status = PTR_ERR(array);
  363. goto out;
  364. }
  365. if (*desc->dir_cookie == 0)
  366. status = nfs_readdir_search_for_pos(array, desc);
  367. else
  368. status = nfs_readdir_search_for_cookie(array, desc);
  369. if (status == -EAGAIN) {
  370. desc->last_cookie = array->last_cookie;
  371. desc->current_index += array->size;
  372. desc->page_index++;
  373. }
  374. nfs_readdir_release_array(desc->page);
  375. out:
  376. return status;
  377. }
  378. /* Fill a page with xdr information before transferring to the cache page */
  379. static
  380. int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
  381. struct nfs_entry *entry, struct file *file, struct inode *inode)
  382. {
  383. struct nfs_open_dir_context *ctx = file->private_data;
  384. struct rpc_cred *cred = ctx->cred;
  385. unsigned long timestamp, gencount;
  386. int error;
  387. again:
  388. timestamp = jiffies;
  389. gencount = nfs_inc_attr_generation_counter();
  390. error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
  391. NFS_SERVER(inode)->dtsize, desc->plus);
  392. if (error < 0) {
  393. /* We requested READDIRPLUS, but the server doesn't grok it */
  394. if (error == -ENOTSUPP && desc->plus) {
  395. NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
  396. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  397. desc->plus = 0;
  398. goto again;
  399. }
  400. goto error;
  401. }
  402. desc->timestamp = timestamp;
  403. desc->gencount = gencount;
  404. error:
  405. return error;
  406. }
  407. static int xdr_decode(nfs_readdir_descriptor_t *desc,
  408. struct nfs_entry *entry, struct xdr_stream *xdr)
  409. {
  410. int error;
  411. error = desc->decode(xdr, entry, desc->plus);
  412. if (error)
  413. return error;
  414. entry->fattr->time_start = desc->timestamp;
  415. entry->fattr->gencount = desc->gencount;
  416. return 0;
  417. }
  418. static
  419. int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
  420. {
  421. if (dentry->d_inode == NULL)
  422. goto different;
  423. if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
  424. goto different;
  425. return 1;
  426. different:
  427. return 0;
  428. }
  429. static
  430. bool nfs_use_readdirplus(struct inode *dir, struct file *filp)
  431. {
  432. if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
  433. return false;
  434. if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
  435. return true;
  436. if (filp->f_pos == 0)
  437. return true;
  438. return false;
  439. }
  440. /*
  441. * This function is called by the lookup code to request the use of
  442. * readdirplus to accelerate any future lookups in the same
  443. * directory.
  444. */
  445. static
  446. void nfs_advise_use_readdirplus(struct inode *dir)
  447. {
  448. set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
  449. }
  450. static
  451. void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
  452. {
  453. struct qstr filename = QSTR_INIT(entry->name, entry->len);
  454. struct dentry *dentry;
  455. struct dentry *alias;
  456. struct inode *dir = parent->d_inode;
  457. struct inode *inode;
  458. if (filename.name[0] == '.') {
  459. if (filename.len == 1)
  460. return;
  461. if (filename.len == 2 && filename.name[1] == '.')
  462. return;
  463. }
  464. filename.hash = full_name_hash(filename.name, filename.len);
  465. dentry = d_lookup(parent, &filename);
  466. if (dentry != NULL) {
  467. if (nfs_same_file(dentry, entry)) {
  468. nfs_refresh_inode(dentry->d_inode, entry->fattr);
  469. goto out;
  470. } else {
  471. d_drop(dentry);
  472. dput(dentry);
  473. }
  474. }
  475. dentry = d_alloc(parent, &filename);
  476. if (dentry == NULL)
  477. return;
  478. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  479. if (IS_ERR(inode))
  480. goto out;
  481. alias = d_materialise_unique(dentry, inode);
  482. if (IS_ERR(alias))
  483. goto out;
  484. else if (alias) {
  485. nfs_set_verifier(alias, nfs_save_change_attribute(dir));
  486. dput(alias);
  487. } else
  488. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  489. out:
  490. dput(dentry);
  491. }
  492. /* Perform conversion from xdr to cache array */
  493. static
  494. int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
  495. struct page **xdr_pages, struct page *page, unsigned int buflen)
  496. {
  497. struct xdr_stream stream;
  498. struct xdr_buf buf;
  499. struct page *scratch;
  500. struct nfs_cache_array *array;
  501. unsigned int count = 0;
  502. int status;
  503. scratch = alloc_page(GFP_KERNEL);
  504. if (scratch == NULL)
  505. return -ENOMEM;
  506. xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
  507. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  508. do {
  509. status = xdr_decode(desc, entry, &stream);
  510. if (status != 0) {
  511. if (status == -EAGAIN)
  512. status = 0;
  513. break;
  514. }
  515. count++;
  516. if (desc->plus != 0)
  517. nfs_prime_dcache(desc->file->f_path.dentry, entry);
  518. status = nfs_readdir_add_to_array(entry, page);
  519. if (status != 0)
  520. break;
  521. } while (!entry->eof);
  522. if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
  523. array = nfs_readdir_get_array(page);
  524. if (!IS_ERR(array)) {
  525. array->eof_index = array->size;
  526. status = 0;
  527. nfs_readdir_release_array(page);
  528. } else
  529. status = PTR_ERR(array);
  530. }
  531. put_page(scratch);
  532. return status;
  533. }
  534. static
  535. void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
  536. {
  537. unsigned int i;
  538. for (i = 0; i < npages; i++)
  539. put_page(pages[i]);
  540. }
  541. static
  542. void nfs_readdir_free_large_page(void *ptr, struct page **pages,
  543. unsigned int npages)
  544. {
  545. nfs_readdir_free_pagearray(pages, npages);
  546. }
  547. /*
  548. * nfs_readdir_large_page will allocate pages that must be freed with a call
  549. * to nfs_readdir_free_large_page
  550. */
  551. static
  552. int nfs_readdir_large_page(struct page **pages, unsigned int npages)
  553. {
  554. unsigned int i;
  555. for (i = 0; i < npages; i++) {
  556. struct page *page = alloc_page(GFP_KERNEL);
  557. if (page == NULL)
  558. goto out_freepages;
  559. pages[i] = page;
  560. }
  561. return 0;
  562. out_freepages:
  563. nfs_readdir_free_pagearray(pages, i);
  564. return -ENOMEM;
  565. }
  566. static
  567. int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
  568. {
  569. struct page *pages[NFS_MAX_READDIR_PAGES];
  570. void *pages_ptr = NULL;
  571. struct nfs_entry entry;
  572. struct file *file = desc->file;
  573. struct nfs_cache_array *array;
  574. int status = -ENOMEM;
  575. unsigned int array_size = ARRAY_SIZE(pages);
  576. entry.prev_cookie = 0;
  577. entry.cookie = desc->last_cookie;
  578. entry.eof = 0;
  579. entry.fh = nfs_alloc_fhandle();
  580. entry.fattr = nfs_alloc_fattr();
  581. entry.server = NFS_SERVER(inode);
  582. if (entry.fh == NULL || entry.fattr == NULL)
  583. goto out;
  584. array = nfs_readdir_get_array(page);
  585. if (IS_ERR(array)) {
  586. status = PTR_ERR(array);
  587. goto out;
  588. }
  589. memset(array, 0, sizeof(struct nfs_cache_array));
  590. array->eof_index = -1;
  591. status = nfs_readdir_large_page(pages, array_size);
  592. if (status < 0)
  593. goto out_release_array;
  594. do {
  595. unsigned int pglen;
  596. status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
  597. if (status < 0)
  598. break;
  599. pglen = status;
  600. status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
  601. if (status < 0) {
  602. if (status == -ENOSPC)
  603. status = 0;
  604. break;
  605. }
  606. } while (array->eof_index < 0);
  607. nfs_readdir_free_large_page(pages_ptr, pages, array_size);
  608. out_release_array:
  609. nfs_readdir_release_array(page);
  610. out:
  611. nfs_free_fattr(entry.fattr);
  612. nfs_free_fhandle(entry.fh);
  613. return status;
  614. }
  615. /*
  616. * Now we cache directories properly, by converting xdr information
  617. * to an array that can be used for lookups later. This results in
  618. * fewer cache pages, since we can store more information on each page.
  619. * We only need to convert from xdr once so future lookups are much simpler
  620. */
  621. static
  622. int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
  623. {
  624. struct inode *inode = desc->file->f_path.dentry->d_inode;
  625. int ret;
  626. ret = nfs_readdir_xdr_to_array(desc, page, inode);
  627. if (ret < 0)
  628. goto error;
  629. SetPageUptodate(page);
  630. if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
  631. /* Should never happen */
  632. nfs_zap_mapping(inode, inode->i_mapping);
  633. }
  634. unlock_page(page);
  635. return 0;
  636. error:
  637. unlock_page(page);
  638. return ret;
  639. }
  640. static
  641. void cache_page_release(nfs_readdir_descriptor_t *desc)
  642. {
  643. if (!desc->page->mapping)
  644. nfs_readdir_clear_array(desc->page);
  645. page_cache_release(desc->page);
  646. desc->page = NULL;
  647. }
  648. static
  649. struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
  650. {
  651. return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
  652. desc->page_index, (filler_t *)nfs_readdir_filler, desc);
  653. }
  654. /*
  655. * Returns 0 if desc->dir_cookie was found on page desc->page_index
  656. */
  657. static
  658. int find_cache_page(nfs_readdir_descriptor_t *desc)
  659. {
  660. int res;
  661. desc->page = get_cache_page(desc);
  662. if (IS_ERR(desc->page))
  663. return PTR_ERR(desc->page);
  664. res = nfs_readdir_search_array(desc);
  665. if (res != 0)
  666. cache_page_release(desc);
  667. return res;
  668. }
  669. /* Search for desc->dir_cookie from the beginning of the page cache */
  670. static inline
  671. int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  672. {
  673. int res;
  674. if (desc->page_index == 0) {
  675. desc->current_index = 0;
  676. desc->last_cookie = 0;
  677. }
  678. do {
  679. res = find_cache_page(desc);
  680. } while (res == -EAGAIN);
  681. return res;
  682. }
  683. /*
  684. * Once we've found the start of the dirent within a page: fill 'er up...
  685. */
  686. static
  687. int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
  688. filldir_t filldir)
  689. {
  690. struct file *file = desc->file;
  691. int i = 0;
  692. int res = 0;
  693. struct nfs_cache_array *array = NULL;
  694. struct nfs_open_dir_context *ctx = file->private_data;
  695. array = nfs_readdir_get_array(desc->page);
  696. if (IS_ERR(array)) {
  697. res = PTR_ERR(array);
  698. goto out;
  699. }
  700. for (i = desc->cache_entry_index; i < array->size; i++) {
  701. struct nfs_cache_array_entry *ent;
  702. ent = &array->array[i];
  703. if (filldir(dirent, ent->string.name, ent->string.len,
  704. file->f_pos, nfs_compat_user_ino64(ent->ino),
  705. ent->d_type) < 0) {
  706. desc->eof = 1;
  707. break;
  708. }
  709. file->f_pos++;
  710. if (i < (array->size-1))
  711. *desc->dir_cookie = array->array[i+1].cookie;
  712. else
  713. *desc->dir_cookie = array->last_cookie;
  714. if (ctx->duped != 0)
  715. ctx->duped = 1;
  716. }
  717. if (array->eof_index >= 0)
  718. desc->eof = 1;
  719. nfs_readdir_release_array(desc->page);
  720. out:
  721. cache_page_release(desc);
  722. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
  723. (unsigned long long)*desc->dir_cookie, res);
  724. return res;
  725. }
  726. /*
  727. * If we cannot find a cookie in our cache, we suspect that this is
  728. * because it points to a deleted file, so we ask the server to return
  729. * whatever it thinks is the next entry. We then feed this to filldir.
  730. * If all goes well, we should then be able to find our way round the
  731. * cache on the next call to readdir_search_pagecache();
  732. *
  733. * NOTE: we cannot add the anonymous page to the pagecache because
  734. * the data it contains might not be page aligned. Besides,
  735. * we should already have a complete representation of the
  736. * directory in the page cache by the time we get here.
  737. */
  738. static inline
  739. int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
  740. filldir_t filldir)
  741. {
  742. struct page *page = NULL;
  743. int status;
  744. struct inode *inode = desc->file->f_path.dentry->d_inode;
  745. struct nfs_open_dir_context *ctx = desc->file->private_data;
  746. dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
  747. (unsigned long long)*desc->dir_cookie);
  748. page = alloc_page(GFP_HIGHUSER);
  749. if (!page) {
  750. status = -ENOMEM;
  751. goto out;
  752. }
  753. desc->page_index = 0;
  754. desc->last_cookie = *desc->dir_cookie;
  755. desc->page = page;
  756. ctx->duped = 0;
  757. status = nfs_readdir_xdr_to_array(desc, page, inode);
  758. if (status < 0)
  759. goto out_release;
  760. status = nfs_do_filldir(desc, dirent, filldir);
  761. out:
  762. dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
  763. __func__, status);
  764. return status;
  765. out_release:
  766. cache_page_release(desc);
  767. goto out;
  768. }
  769. /* The file offset position represents the dirent entry number. A
  770. last cookie cache takes care of the common case of reading the
  771. whole directory.
  772. */
  773. static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  774. {
  775. struct dentry *dentry = filp->f_path.dentry;
  776. struct inode *inode = dentry->d_inode;
  777. nfs_readdir_descriptor_t my_desc,
  778. *desc = &my_desc;
  779. struct nfs_open_dir_context *dir_ctx = filp->private_data;
  780. int res;
  781. dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
  782. dentry->d_parent->d_name.name, dentry->d_name.name,
  783. (long long)filp->f_pos);
  784. nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
  785. /*
  786. * filp->f_pos points to the dirent entry number.
  787. * *desc->dir_cookie has the cookie for the next entry. We have
  788. * to either find the entry with the appropriate number or
  789. * revalidate the cookie.
  790. */
  791. memset(desc, 0, sizeof(*desc));
  792. desc->file = filp;
  793. desc->dir_cookie = &dir_ctx->dir_cookie;
  794. desc->decode = NFS_PROTO(inode)->decode_dirent;
  795. desc->plus = nfs_use_readdirplus(inode, filp) ? 1 : 0;
  796. nfs_block_sillyrename(dentry);
  797. res = nfs_revalidate_mapping(inode, filp->f_mapping);
  798. if (res < 0)
  799. goto out;
  800. do {
  801. res = readdir_search_pagecache(desc);
  802. if (res == -EBADCOOKIE) {
  803. res = 0;
  804. /* This means either end of directory */
  805. if (*desc->dir_cookie && desc->eof == 0) {
  806. /* Or that the server has 'lost' a cookie */
  807. res = uncached_readdir(desc, dirent, filldir);
  808. if (res == 0)
  809. continue;
  810. }
  811. break;
  812. }
  813. if (res == -ETOOSMALL && desc->plus) {
  814. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  815. nfs_zap_caches(inode);
  816. desc->page_index = 0;
  817. desc->plus = 0;
  818. desc->eof = 0;
  819. continue;
  820. }
  821. if (res < 0)
  822. break;
  823. res = nfs_do_filldir(desc, dirent, filldir);
  824. if (res < 0)
  825. break;
  826. } while (!desc->eof);
  827. out:
  828. nfs_unblock_sillyrename(dentry);
  829. if (res > 0)
  830. res = 0;
  831. dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
  832. dentry->d_parent->d_name.name, dentry->d_name.name,
  833. res);
  834. return res;
  835. }
  836. static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
  837. {
  838. struct dentry *dentry = filp->f_path.dentry;
  839. struct inode *inode = dentry->d_inode;
  840. struct nfs_open_dir_context *dir_ctx = filp->private_data;
  841. dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
  842. dentry->d_parent->d_name.name,
  843. dentry->d_name.name,
  844. offset, origin);
  845. mutex_lock(&inode->i_mutex);
  846. switch (origin) {
  847. case 1:
  848. offset += filp->f_pos;
  849. case 0:
  850. if (offset >= 0)
  851. break;
  852. default:
  853. offset = -EINVAL;
  854. goto out;
  855. }
  856. if (offset != filp->f_pos) {
  857. filp->f_pos = offset;
  858. dir_ctx->dir_cookie = 0;
  859. dir_ctx->duped = 0;
  860. }
  861. out:
  862. mutex_unlock(&inode->i_mutex);
  863. return offset;
  864. }
  865. /*
  866. * All directory operations under NFS are synchronous, so fsync()
  867. * is a dummy operation.
  868. */
  869. static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
  870. int datasync)
  871. {
  872. struct dentry *dentry = filp->f_path.dentry;
  873. struct inode *inode = dentry->d_inode;
  874. dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
  875. dentry->d_parent->d_name.name, dentry->d_name.name,
  876. datasync);
  877. mutex_lock(&inode->i_mutex);
  878. nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
  879. mutex_unlock(&inode->i_mutex);
  880. return 0;
  881. }
  882. /**
  883. * nfs_force_lookup_revalidate - Mark the directory as having changed
  884. * @dir - pointer to directory inode
  885. *
  886. * This forces the revalidation code in nfs_lookup_revalidate() to do a
  887. * full lookup on all child dentries of 'dir' whenever a change occurs
  888. * on the server that might have invalidated our dcache.
  889. *
  890. * The caller should be holding dir->i_lock
  891. */
  892. void nfs_force_lookup_revalidate(struct inode *dir)
  893. {
  894. NFS_I(dir)->cache_change_attribute++;
  895. }
  896. /*
  897. * A check for whether or not the parent directory has changed.
  898. * In the case it has, we assume that the dentries are untrustworthy
  899. * and may need to be looked up again.
  900. */
  901. static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
  902. {
  903. if (IS_ROOT(dentry))
  904. return 1;
  905. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
  906. return 0;
  907. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  908. return 0;
  909. /* Revalidate nfsi->cache_change_attribute before we declare a match */
  910. if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
  911. return 0;
  912. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  913. return 0;
  914. return 1;
  915. }
  916. /*
  917. * Return the intent data that applies to this particular path component
  918. *
  919. * Note that the current set of intents only apply to the very last
  920. * component of the path and none of them is set before that last
  921. * component.
  922. */
  923. static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd,
  924. unsigned int mask)
  925. {
  926. return nd->flags & mask;
  927. }
  928. /*
  929. * Use intent information to check whether or not we're going to do
  930. * an O_EXCL create using this path component.
  931. */
  932. static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
  933. {
  934. if (NFS_PROTO(dir)->version == 2)
  935. return 0;
  936. return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
  937. }
  938. /*
  939. * Inode and filehandle revalidation for lookups.
  940. *
  941. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  942. * or if the intent information indicates that we're about to open this
  943. * particular file and the "nocto" mount flag is not set.
  944. *
  945. */
  946. static inline
  947. int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
  948. {
  949. struct nfs_server *server = NFS_SERVER(inode);
  950. if (IS_AUTOMOUNT(inode))
  951. return 0;
  952. if (nd != NULL) {
  953. /* VFS wants an on-the-wire revalidation */
  954. if (nd->flags & LOOKUP_REVAL)
  955. goto out_force;
  956. /* This is an open(2) */
  957. if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
  958. !(server->flags & NFS_MOUNT_NOCTO) &&
  959. (S_ISREG(inode->i_mode) ||
  960. S_ISDIR(inode->i_mode)))
  961. goto out_force;
  962. return 0;
  963. }
  964. return nfs_revalidate_inode(server, inode);
  965. out_force:
  966. return __nfs_revalidate_inode(server, inode);
  967. }
  968. /*
  969. * We judge how long we want to trust negative
  970. * dentries by looking at the parent inode mtime.
  971. *
  972. * If parent mtime has changed, we revalidate, else we wait for a
  973. * period corresponding to the parent's attribute cache timeout value.
  974. */
  975. static inline
  976. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  977. struct nameidata *nd)
  978. {
  979. /* Don't revalidate a negative dentry if we're creating a new file */
  980. if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
  981. return 0;
  982. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
  983. return 1;
  984. return !nfs_check_verifier(dir, dentry);
  985. }
  986. /*
  987. * This is called every time the dcache has a lookup hit,
  988. * and we should check whether we can really trust that
  989. * lookup.
  990. *
  991. * NOTE! The hit can be a negative hit too, don't assume
  992. * we have an inode!
  993. *
  994. * If the parent directory is seen to have changed, we throw out the
  995. * cached dentry and do a new lookup.
  996. */
  997. static int nfs_lookup_revalidate(struct dentry *dentry, struct nameidata *nd)
  998. {
  999. struct inode *dir;
  1000. struct inode *inode;
  1001. struct dentry *parent;
  1002. struct nfs_fh *fhandle = NULL;
  1003. struct nfs_fattr *fattr = NULL;
  1004. int error;
  1005. if (nd->flags & LOOKUP_RCU)
  1006. return -ECHILD;
  1007. parent = dget_parent(dentry);
  1008. dir = parent->d_inode;
  1009. nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
  1010. inode = dentry->d_inode;
  1011. if (!inode) {
  1012. if (nfs_neg_need_reval(dir, dentry, nd))
  1013. goto out_bad;
  1014. goto out_valid_noent;
  1015. }
  1016. if (is_bad_inode(inode)) {
  1017. dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
  1018. __func__, dentry->d_parent->d_name.name,
  1019. dentry->d_name.name);
  1020. goto out_bad;
  1021. }
  1022. if (nfs_have_delegation(inode, FMODE_READ))
  1023. goto out_set_verifier;
  1024. /* Force a full look up iff the parent directory has changed */
  1025. if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
  1026. if (nfs_lookup_verify_inode(inode, nd))
  1027. goto out_zap_parent;
  1028. goto out_valid;
  1029. }
  1030. if (NFS_STALE(inode))
  1031. goto out_bad;
  1032. error = -ENOMEM;
  1033. fhandle = nfs_alloc_fhandle();
  1034. fattr = nfs_alloc_fattr();
  1035. if (fhandle == NULL || fattr == NULL)
  1036. goto out_error;
  1037. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1038. if (error)
  1039. goto out_bad;
  1040. if (nfs_compare_fh(NFS_FH(inode), fhandle))
  1041. goto out_bad;
  1042. if ((error = nfs_refresh_inode(inode, fattr)) != 0)
  1043. goto out_bad;
  1044. nfs_free_fattr(fattr);
  1045. nfs_free_fhandle(fhandle);
  1046. out_set_verifier:
  1047. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1048. out_valid:
  1049. /* Success: notify readdir to use READDIRPLUS */
  1050. nfs_advise_use_readdirplus(dir);
  1051. out_valid_noent:
  1052. dput(parent);
  1053. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
  1054. __func__, dentry->d_parent->d_name.name,
  1055. dentry->d_name.name);
  1056. return 1;
  1057. out_zap_parent:
  1058. nfs_zap_caches(dir);
  1059. out_bad:
  1060. nfs_mark_for_revalidate(dir);
  1061. if (inode && S_ISDIR(inode->i_mode)) {
  1062. /* Purge readdir caches. */
  1063. nfs_zap_caches(inode);
  1064. /* If we have submounts, don't unhash ! */
  1065. if (have_submounts(dentry))
  1066. goto out_valid;
  1067. if (dentry->d_flags & DCACHE_DISCONNECTED)
  1068. goto out_valid;
  1069. shrink_dcache_parent(dentry);
  1070. }
  1071. d_drop(dentry);
  1072. nfs_free_fattr(fattr);
  1073. nfs_free_fhandle(fhandle);
  1074. dput(parent);
  1075. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
  1076. __func__, dentry->d_parent->d_name.name,
  1077. dentry->d_name.name);
  1078. return 0;
  1079. out_error:
  1080. nfs_free_fattr(fattr);
  1081. nfs_free_fhandle(fhandle);
  1082. dput(parent);
  1083. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
  1084. __func__, dentry->d_parent->d_name.name,
  1085. dentry->d_name.name, error);
  1086. return error;
  1087. }
  1088. /*
  1089. * This is called from dput() when d_count is going to 0.
  1090. */
  1091. static int nfs_dentry_delete(const struct dentry *dentry)
  1092. {
  1093. dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
  1094. dentry->d_parent->d_name.name, dentry->d_name.name,
  1095. dentry->d_flags);
  1096. /* Unhash any dentry with a stale inode */
  1097. if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
  1098. return 1;
  1099. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1100. /* Unhash it, so that ->d_iput() would be called */
  1101. return 1;
  1102. }
  1103. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  1104. /* Unhash it, so that ancestors of killed async unlink
  1105. * files will be cleaned up during umount */
  1106. return 1;
  1107. }
  1108. return 0;
  1109. }
  1110. static void nfs_drop_nlink(struct inode *inode)
  1111. {
  1112. spin_lock(&inode->i_lock);
  1113. if (inode->i_nlink > 0)
  1114. drop_nlink(inode);
  1115. spin_unlock(&inode->i_lock);
  1116. }
  1117. /*
  1118. * Called when the dentry loses inode.
  1119. * We use it to clean up silly-renamed files.
  1120. */
  1121. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  1122. {
  1123. if (S_ISDIR(inode->i_mode))
  1124. /* drop any readdir cache as it could easily be old */
  1125. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  1126. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1127. drop_nlink(inode);
  1128. nfs_complete_unlink(dentry, inode);
  1129. }
  1130. iput(inode);
  1131. }
  1132. static void nfs_d_release(struct dentry *dentry)
  1133. {
  1134. /* free cached devname value, if it survived that far */
  1135. if (unlikely(dentry->d_fsdata)) {
  1136. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1137. WARN_ON(1);
  1138. else
  1139. kfree(dentry->d_fsdata);
  1140. }
  1141. }
  1142. const struct dentry_operations nfs_dentry_operations = {
  1143. .d_revalidate = nfs_lookup_revalidate,
  1144. .d_delete = nfs_dentry_delete,
  1145. .d_iput = nfs_dentry_iput,
  1146. .d_automount = nfs_d_automount,
  1147. .d_release = nfs_d_release,
  1148. };
  1149. static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1150. {
  1151. struct dentry *res;
  1152. struct dentry *parent;
  1153. struct inode *inode = NULL;
  1154. struct nfs_fh *fhandle = NULL;
  1155. struct nfs_fattr *fattr = NULL;
  1156. int error;
  1157. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  1158. dentry->d_parent->d_name.name, dentry->d_name.name);
  1159. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  1160. res = ERR_PTR(-ENAMETOOLONG);
  1161. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  1162. goto out;
  1163. /*
  1164. * If we're doing an exclusive create, optimize away the lookup
  1165. * but don't hash the dentry.
  1166. */
  1167. if (nfs_is_exclusive_create(dir, nd)) {
  1168. d_instantiate(dentry, NULL);
  1169. res = NULL;
  1170. goto out;
  1171. }
  1172. res = ERR_PTR(-ENOMEM);
  1173. fhandle = nfs_alloc_fhandle();
  1174. fattr = nfs_alloc_fattr();
  1175. if (fhandle == NULL || fattr == NULL)
  1176. goto out;
  1177. parent = dentry->d_parent;
  1178. /* Protect against concurrent sillydeletes */
  1179. nfs_block_sillyrename(parent);
  1180. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1181. if (error == -ENOENT)
  1182. goto no_entry;
  1183. if (error < 0) {
  1184. res = ERR_PTR(error);
  1185. goto out_unblock_sillyrename;
  1186. }
  1187. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1188. res = ERR_CAST(inode);
  1189. if (IS_ERR(res))
  1190. goto out_unblock_sillyrename;
  1191. /* Success: notify readdir to use READDIRPLUS */
  1192. nfs_advise_use_readdirplus(dir);
  1193. no_entry:
  1194. res = d_materialise_unique(dentry, inode);
  1195. if (res != NULL) {
  1196. if (IS_ERR(res))
  1197. goto out_unblock_sillyrename;
  1198. dentry = res;
  1199. }
  1200. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1201. out_unblock_sillyrename:
  1202. nfs_unblock_sillyrename(parent);
  1203. out:
  1204. nfs_free_fattr(fattr);
  1205. nfs_free_fhandle(fhandle);
  1206. return res;
  1207. }
  1208. #ifdef CONFIG_NFS_V4
  1209. static int nfs4_lookup_revalidate(struct dentry *, struct nameidata *);
  1210. const struct dentry_operations nfs4_dentry_operations = {
  1211. .d_revalidate = nfs4_lookup_revalidate,
  1212. .d_delete = nfs_dentry_delete,
  1213. .d_iput = nfs_dentry_iput,
  1214. .d_automount = nfs_d_automount,
  1215. .d_release = nfs_d_release,
  1216. };
  1217. /*
  1218. * Use intent information to determine whether we need to substitute
  1219. * the NFSv4-style stateful OPEN for the LOOKUP call
  1220. */
  1221. static int is_atomic_open(struct nameidata *nd)
  1222. {
  1223. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
  1224. return 0;
  1225. /* NFS does not (yet) have a stateful open for directories */
  1226. if (nd->flags & LOOKUP_DIRECTORY)
  1227. return 0;
  1228. /* Are we trying to write to a read only partition? */
  1229. if (__mnt_is_readonly(nd->path.mnt) &&
  1230. (nd->intent.open.flags & (O_CREAT|O_TRUNC|O_ACCMODE)))
  1231. return 0;
  1232. return 1;
  1233. }
  1234. static fmode_t flags_to_mode(int flags)
  1235. {
  1236. fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
  1237. if ((flags & O_ACCMODE) != O_WRONLY)
  1238. res |= FMODE_READ;
  1239. if ((flags & O_ACCMODE) != O_RDONLY)
  1240. res |= FMODE_WRITE;
  1241. return res;
  1242. }
  1243. static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
  1244. {
  1245. return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
  1246. }
  1247. static int do_open(struct inode *inode, struct file *filp)
  1248. {
  1249. nfs_fscache_set_inode_cookie(inode, filp);
  1250. return 0;
  1251. }
  1252. static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
  1253. {
  1254. struct file *filp;
  1255. int ret = 0;
  1256. /* If the open_intent is for execute, we have an extra check to make */
  1257. if (ctx->mode & FMODE_EXEC) {
  1258. ret = nfs_may_open(ctx->dentry->d_inode,
  1259. ctx->cred,
  1260. nd->intent.open.flags);
  1261. if (ret < 0)
  1262. goto out;
  1263. }
  1264. filp = lookup_instantiate_filp(nd, ctx->dentry, do_open);
  1265. if (IS_ERR(filp))
  1266. ret = PTR_ERR(filp);
  1267. else
  1268. nfs_file_set_open_context(filp, ctx);
  1269. out:
  1270. put_nfs_open_context(ctx);
  1271. return ret;
  1272. }
  1273. static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  1274. {
  1275. struct nfs_open_context *ctx;
  1276. struct iattr attr;
  1277. struct dentry *res = NULL;
  1278. struct inode *inode;
  1279. int open_flags;
  1280. int err;
  1281. dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
  1282. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1283. /* Check that we are indeed trying to open this file */
  1284. if (!is_atomic_open(nd))
  1285. goto no_open;
  1286. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  1287. res = ERR_PTR(-ENAMETOOLONG);
  1288. goto out;
  1289. }
  1290. /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
  1291. * the dentry. */
  1292. if (nd->flags & LOOKUP_EXCL) {
  1293. d_instantiate(dentry, NULL);
  1294. goto out;
  1295. }
  1296. open_flags = nd->intent.open.flags;
  1297. attr.ia_valid = ATTR_OPEN;
  1298. ctx = create_nfs_open_context(dentry, open_flags);
  1299. res = ERR_CAST(ctx);
  1300. if (IS_ERR(ctx))
  1301. goto out;
  1302. if (nd->flags & LOOKUP_CREATE) {
  1303. attr.ia_mode = nd->intent.open.create_mode;
  1304. attr.ia_valid |= ATTR_MODE;
  1305. attr.ia_mode &= ~current_umask();
  1306. } else
  1307. open_flags &= ~(O_EXCL | O_CREAT);
  1308. if (open_flags & O_TRUNC) {
  1309. attr.ia_valid |= ATTR_SIZE;
  1310. attr.ia_size = 0;
  1311. }
  1312. /* Open the file on the server */
  1313. nfs_block_sillyrename(dentry->d_parent);
  1314. inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
  1315. if (IS_ERR(inode)) {
  1316. nfs_unblock_sillyrename(dentry->d_parent);
  1317. put_nfs_open_context(ctx);
  1318. switch (PTR_ERR(inode)) {
  1319. /* Make a negative dentry */
  1320. case -ENOENT:
  1321. d_add(dentry, NULL);
  1322. res = NULL;
  1323. goto out;
  1324. /* This turned out not to be a regular file */
  1325. case -EISDIR:
  1326. case -ENOTDIR:
  1327. goto no_open;
  1328. case -ELOOP:
  1329. if (!(nd->intent.open.flags & O_NOFOLLOW))
  1330. goto no_open;
  1331. /* case -EINVAL: */
  1332. default:
  1333. res = ERR_CAST(inode);
  1334. goto out;
  1335. }
  1336. }
  1337. res = d_add_unique(dentry, inode);
  1338. nfs_unblock_sillyrename(dentry->d_parent);
  1339. if (res != NULL) {
  1340. dput(ctx->dentry);
  1341. ctx->dentry = dget(res);
  1342. dentry = res;
  1343. }
  1344. err = nfs_intent_set_file(nd, ctx);
  1345. if (err < 0) {
  1346. if (res != NULL)
  1347. dput(res);
  1348. return ERR_PTR(err);
  1349. }
  1350. out:
  1351. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1352. return res;
  1353. no_open:
  1354. return nfs_lookup(dir, dentry, nd);
  1355. }
  1356. static int nfs4_lookup_revalidate(struct dentry *dentry, struct nameidata *nd)
  1357. {
  1358. struct dentry *parent = NULL;
  1359. struct inode *inode;
  1360. struct inode *dir;
  1361. int openflags, ret = 0;
  1362. if (nd->flags & LOOKUP_RCU)
  1363. return -ECHILD;
  1364. inode = dentry->d_inode;
  1365. if (!is_atomic_open(nd) || d_mountpoint(dentry))
  1366. goto no_open;
  1367. parent = dget_parent(dentry);
  1368. dir = parent->d_inode;
  1369. /* We can't create new files in nfs_open_revalidate(), so we
  1370. * optimize away revalidation of negative dentries.
  1371. */
  1372. if (inode == NULL) {
  1373. if (!nfs_neg_need_reval(dir, dentry, nd))
  1374. ret = 1;
  1375. goto out;
  1376. }
  1377. /* NFS only supports OPEN on regular files */
  1378. if (!S_ISREG(inode->i_mode))
  1379. goto no_open_dput;
  1380. openflags = nd->intent.open.flags;
  1381. /* We cannot do exclusive creation on a positive dentry */
  1382. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  1383. goto no_open_dput;
  1384. /* Let f_op->open() actually open (and revalidate) the file */
  1385. ret = 1;
  1386. out:
  1387. dput(parent);
  1388. return ret;
  1389. no_open_dput:
  1390. dput(parent);
  1391. no_open:
  1392. return nfs_lookup_revalidate(dentry, nd);
  1393. }
  1394. static int nfs_open_create(struct inode *dir, struct dentry *dentry,
  1395. umode_t mode, struct nameidata *nd)
  1396. {
  1397. struct nfs_open_context *ctx = NULL;
  1398. struct iattr attr;
  1399. int error;
  1400. int open_flags = O_CREAT|O_EXCL;
  1401. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1402. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1403. attr.ia_mode = mode;
  1404. attr.ia_valid = ATTR_MODE;
  1405. if (nd)
  1406. open_flags = nd->intent.open.flags;
  1407. ctx = create_nfs_open_context(dentry, open_flags);
  1408. error = PTR_ERR(ctx);
  1409. if (IS_ERR(ctx))
  1410. goto out_err_drop;
  1411. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
  1412. if (error != 0)
  1413. goto out_put_ctx;
  1414. if (nd) {
  1415. error = nfs_intent_set_file(nd, ctx);
  1416. if (error < 0)
  1417. goto out_err;
  1418. } else {
  1419. put_nfs_open_context(ctx);
  1420. }
  1421. return 0;
  1422. out_put_ctx:
  1423. put_nfs_open_context(ctx);
  1424. out_err_drop:
  1425. d_drop(dentry);
  1426. out_err:
  1427. return error;
  1428. }
  1429. #endif /* CONFIG_NFSV4 */
  1430. /*
  1431. * Code common to create, mkdir, and mknod.
  1432. */
  1433. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1434. struct nfs_fattr *fattr)
  1435. {
  1436. struct dentry *parent = dget_parent(dentry);
  1437. struct inode *dir = parent->d_inode;
  1438. struct inode *inode;
  1439. int error = -EACCES;
  1440. d_drop(dentry);
  1441. /* We may have been initialized further down */
  1442. if (dentry->d_inode)
  1443. goto out;
  1444. if (fhandle->size == 0) {
  1445. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1446. if (error)
  1447. goto out_error;
  1448. }
  1449. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1450. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1451. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1452. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
  1453. if (error < 0)
  1454. goto out_error;
  1455. }
  1456. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1457. error = PTR_ERR(inode);
  1458. if (IS_ERR(inode))
  1459. goto out_error;
  1460. d_add(dentry, inode);
  1461. out:
  1462. dput(parent);
  1463. return 0;
  1464. out_error:
  1465. nfs_mark_for_revalidate(dir);
  1466. dput(parent);
  1467. return error;
  1468. }
  1469. /*
  1470. * Following a failed create operation, we drop the dentry rather
  1471. * than retain a negative dentry. This avoids a problem in the event
  1472. * that the operation succeeded on the server, but an error in the
  1473. * reply path made it appear to have failed.
  1474. */
  1475. static int nfs_create(struct inode *dir, struct dentry *dentry,
  1476. umode_t mode, struct nameidata *nd)
  1477. {
  1478. struct iattr attr;
  1479. int error;
  1480. int open_flags = O_CREAT|O_EXCL;
  1481. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1482. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1483. attr.ia_mode = mode;
  1484. attr.ia_valid = ATTR_MODE;
  1485. if (nd)
  1486. open_flags = nd->intent.open.flags;
  1487. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, NULL);
  1488. if (error != 0)
  1489. goto out_err;
  1490. return 0;
  1491. out_err:
  1492. d_drop(dentry);
  1493. return error;
  1494. }
  1495. /*
  1496. * See comments for nfs_proc_create regarding failed operations.
  1497. */
  1498. static int
  1499. nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  1500. {
  1501. struct iattr attr;
  1502. int status;
  1503. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1504. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1505. if (!new_valid_dev(rdev))
  1506. return -EINVAL;
  1507. attr.ia_mode = mode;
  1508. attr.ia_valid = ATTR_MODE;
  1509. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1510. if (status != 0)
  1511. goto out_err;
  1512. return 0;
  1513. out_err:
  1514. d_drop(dentry);
  1515. return status;
  1516. }
  1517. /*
  1518. * See comments for nfs_proc_create regarding failed operations.
  1519. */
  1520. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1521. {
  1522. struct iattr attr;
  1523. int error;
  1524. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1525. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1526. attr.ia_valid = ATTR_MODE;
  1527. attr.ia_mode = mode | S_IFDIR;
  1528. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1529. if (error != 0)
  1530. goto out_err;
  1531. return 0;
  1532. out_err:
  1533. d_drop(dentry);
  1534. return error;
  1535. }
  1536. static void nfs_dentry_handle_enoent(struct dentry *dentry)
  1537. {
  1538. if (dentry->d_inode != NULL && !d_unhashed(dentry))
  1539. d_delete(dentry);
  1540. }
  1541. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1542. {
  1543. int error;
  1544. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1545. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1546. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1547. /* Ensure the VFS deletes this inode */
  1548. if (error == 0 && dentry->d_inode != NULL)
  1549. clear_nlink(dentry->d_inode);
  1550. else if (error == -ENOENT)
  1551. nfs_dentry_handle_enoent(dentry);
  1552. return error;
  1553. }
  1554. /*
  1555. * Remove a file after making sure there are no pending writes,
  1556. * and after checking that the file has only one user.
  1557. *
  1558. * We invalidate the attribute cache and free the inode prior to the operation
  1559. * to avoid possible races if the server reuses the inode.
  1560. */
  1561. static int nfs_safe_remove(struct dentry *dentry)
  1562. {
  1563. struct inode *dir = dentry->d_parent->d_inode;
  1564. struct inode *inode = dentry->d_inode;
  1565. int error = -EBUSY;
  1566. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1567. dentry->d_parent->d_name.name, dentry->d_name.name);
  1568. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1569. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1570. error = 0;
  1571. goto out;
  1572. }
  1573. if (inode != NULL) {
  1574. nfs_inode_return_delegation(inode);
  1575. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1576. /* The VFS may want to delete this inode */
  1577. if (error == 0)
  1578. nfs_drop_nlink(inode);
  1579. nfs_mark_for_revalidate(inode);
  1580. } else
  1581. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1582. if (error == -ENOENT)
  1583. nfs_dentry_handle_enoent(dentry);
  1584. out:
  1585. return error;
  1586. }
  1587. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1588. * belongs to an active ".nfs..." file and we return -EBUSY.
  1589. *
  1590. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1591. */
  1592. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1593. {
  1594. int error;
  1595. int need_rehash = 0;
  1596. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1597. dir->i_ino, dentry->d_name.name);
  1598. spin_lock(&dentry->d_lock);
  1599. if (dentry->d_count > 1) {
  1600. spin_unlock(&dentry->d_lock);
  1601. /* Start asynchronous writeout of the inode */
  1602. write_inode_now(dentry->d_inode, 0);
  1603. error = nfs_sillyrename(dir, dentry);
  1604. return error;
  1605. }
  1606. if (!d_unhashed(dentry)) {
  1607. __d_drop(dentry);
  1608. need_rehash = 1;
  1609. }
  1610. spin_unlock(&dentry->d_lock);
  1611. error = nfs_safe_remove(dentry);
  1612. if (!error || error == -ENOENT) {
  1613. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1614. } else if (need_rehash)
  1615. d_rehash(dentry);
  1616. return error;
  1617. }
  1618. /*
  1619. * To create a symbolic link, most file systems instantiate a new inode,
  1620. * add a page to it containing the path, then write it out to the disk
  1621. * using prepare_write/commit_write.
  1622. *
  1623. * Unfortunately the NFS client can't create the in-core inode first
  1624. * because it needs a file handle to create an in-core inode (see
  1625. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1626. * symlink request has completed on the server.
  1627. *
  1628. * So instead we allocate a raw page, copy the symname into it, then do
  1629. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1630. * now have a new file handle and can instantiate an in-core NFS inode
  1631. * and move the raw page into its mapping.
  1632. */
  1633. static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1634. {
  1635. struct pagevec lru_pvec;
  1636. struct page *page;
  1637. char *kaddr;
  1638. struct iattr attr;
  1639. unsigned int pathlen = strlen(symname);
  1640. int error;
  1641. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1642. dir->i_ino, dentry->d_name.name, symname);
  1643. if (pathlen > PAGE_SIZE)
  1644. return -ENAMETOOLONG;
  1645. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1646. attr.ia_valid = ATTR_MODE;
  1647. page = alloc_page(GFP_HIGHUSER);
  1648. if (!page)
  1649. return -ENOMEM;
  1650. kaddr = kmap_atomic(page);
  1651. memcpy(kaddr, symname, pathlen);
  1652. if (pathlen < PAGE_SIZE)
  1653. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1654. kunmap_atomic(kaddr);
  1655. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1656. if (error != 0) {
  1657. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1658. dir->i_sb->s_id, dir->i_ino,
  1659. dentry->d_name.name, symname, error);
  1660. d_drop(dentry);
  1661. __free_page(page);
  1662. return error;
  1663. }
  1664. /*
  1665. * No big deal if we can't add this page to the page cache here.
  1666. * READLINK will get the missing page from the server if needed.
  1667. */
  1668. pagevec_init(&lru_pvec, 0);
  1669. if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
  1670. GFP_KERNEL)) {
  1671. pagevec_add(&lru_pvec, page);
  1672. pagevec_lru_add_file(&lru_pvec);
  1673. SetPageUptodate(page);
  1674. unlock_page(page);
  1675. } else
  1676. __free_page(page);
  1677. return 0;
  1678. }
  1679. static int
  1680. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1681. {
  1682. struct inode *inode = old_dentry->d_inode;
  1683. int error;
  1684. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1685. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1686. dentry->d_parent->d_name.name, dentry->d_name.name);
  1687. nfs_inode_return_delegation(inode);
  1688. d_drop(dentry);
  1689. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1690. if (error == 0) {
  1691. ihold(inode);
  1692. d_add(dentry, inode);
  1693. }
  1694. return error;
  1695. }
  1696. /*
  1697. * RENAME
  1698. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1699. * different file handle for the same inode after a rename (e.g. when
  1700. * moving to a different directory). A fail-safe method to do so would
  1701. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1702. * rename the old file using the sillyrename stuff. This way, the original
  1703. * file in old_dir will go away when the last process iput()s the inode.
  1704. *
  1705. * FIXED.
  1706. *
  1707. * It actually works quite well. One needs to have the possibility for
  1708. * at least one ".nfs..." file in each directory the file ever gets
  1709. * moved or linked to which happens automagically with the new
  1710. * implementation that only depends on the dcache stuff instead of
  1711. * using the inode layer
  1712. *
  1713. * Unfortunately, things are a little more complicated than indicated
  1714. * above. For a cross-directory move, we want to make sure we can get
  1715. * rid of the old inode after the operation. This means there must be
  1716. * no pending writes (if it's a file), and the use count must be 1.
  1717. * If these conditions are met, we can drop the dentries before doing
  1718. * the rename.
  1719. */
  1720. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1721. struct inode *new_dir, struct dentry *new_dentry)
  1722. {
  1723. struct inode *old_inode = old_dentry->d_inode;
  1724. struct inode *new_inode = new_dentry->d_inode;
  1725. struct dentry *dentry = NULL, *rehash = NULL;
  1726. int error = -EBUSY;
  1727. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1728. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1729. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1730. new_dentry->d_count);
  1731. /*
  1732. * For non-directories, check whether the target is busy and if so,
  1733. * make a copy of the dentry and then do a silly-rename. If the
  1734. * silly-rename succeeds, the copied dentry is hashed and becomes
  1735. * the new target.
  1736. */
  1737. if (new_inode && !S_ISDIR(new_inode->i_mode)) {
  1738. /*
  1739. * To prevent any new references to the target during the
  1740. * rename, we unhash the dentry in advance.
  1741. */
  1742. if (!d_unhashed(new_dentry)) {
  1743. d_drop(new_dentry);
  1744. rehash = new_dentry;
  1745. }
  1746. if (new_dentry->d_count > 2) {
  1747. int err;
  1748. /* copy the target dentry's name */
  1749. dentry = d_alloc(new_dentry->d_parent,
  1750. &new_dentry->d_name);
  1751. if (!dentry)
  1752. goto out;
  1753. /* silly-rename the existing target ... */
  1754. err = nfs_sillyrename(new_dir, new_dentry);
  1755. if (err)
  1756. goto out;
  1757. new_dentry = dentry;
  1758. rehash = NULL;
  1759. new_inode = NULL;
  1760. }
  1761. }
  1762. nfs_inode_return_delegation(old_inode);
  1763. if (new_inode != NULL)
  1764. nfs_inode_return_delegation(new_inode);
  1765. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1766. new_dir, &new_dentry->d_name);
  1767. nfs_mark_for_revalidate(old_inode);
  1768. out:
  1769. if (rehash)
  1770. d_rehash(rehash);
  1771. if (!error) {
  1772. if (new_inode != NULL)
  1773. nfs_drop_nlink(new_inode);
  1774. d_move(old_dentry, new_dentry);
  1775. nfs_set_verifier(new_dentry,
  1776. nfs_save_change_attribute(new_dir));
  1777. } else if (error == -ENOENT)
  1778. nfs_dentry_handle_enoent(old_dentry);
  1779. /* new dentry created? */
  1780. if (dentry)
  1781. dput(dentry);
  1782. return error;
  1783. }
  1784. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1785. static LIST_HEAD(nfs_access_lru_list);
  1786. static atomic_long_t nfs_access_nr_entries;
  1787. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1788. {
  1789. put_rpccred(entry->cred);
  1790. kfree(entry);
  1791. smp_mb__before_atomic_dec();
  1792. atomic_long_dec(&nfs_access_nr_entries);
  1793. smp_mb__after_atomic_dec();
  1794. }
  1795. static void nfs_access_free_list(struct list_head *head)
  1796. {
  1797. struct nfs_access_entry *cache;
  1798. while (!list_empty(head)) {
  1799. cache = list_entry(head->next, struct nfs_access_entry, lru);
  1800. list_del(&cache->lru);
  1801. nfs_access_free_entry(cache);
  1802. }
  1803. }
  1804. int nfs_access_cache_shrinker(struct shrinker *shrink,
  1805. struct shrink_control *sc)
  1806. {
  1807. LIST_HEAD(head);
  1808. struct nfs_inode *nfsi, *next;
  1809. struct nfs_access_entry *cache;
  1810. int nr_to_scan = sc->nr_to_scan;
  1811. gfp_t gfp_mask = sc->gfp_mask;
  1812. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  1813. return (nr_to_scan == 0) ? 0 : -1;
  1814. spin_lock(&nfs_access_lru_lock);
  1815. list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
  1816. struct inode *inode;
  1817. if (nr_to_scan-- == 0)
  1818. break;
  1819. inode = &nfsi->vfs_inode;
  1820. spin_lock(&inode->i_lock);
  1821. if (list_empty(&nfsi->access_cache_entry_lru))
  1822. goto remove_lru_entry;
  1823. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1824. struct nfs_access_entry, lru);
  1825. list_move(&cache->lru, &head);
  1826. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1827. if (!list_empty(&nfsi->access_cache_entry_lru))
  1828. list_move_tail(&nfsi->access_cache_inode_lru,
  1829. &nfs_access_lru_list);
  1830. else {
  1831. remove_lru_entry:
  1832. list_del_init(&nfsi->access_cache_inode_lru);
  1833. smp_mb__before_clear_bit();
  1834. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1835. smp_mb__after_clear_bit();
  1836. }
  1837. spin_unlock(&inode->i_lock);
  1838. }
  1839. spin_unlock(&nfs_access_lru_lock);
  1840. nfs_access_free_list(&head);
  1841. return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
  1842. }
  1843. static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
  1844. {
  1845. struct rb_root *root_node = &nfsi->access_cache;
  1846. struct rb_node *n;
  1847. struct nfs_access_entry *entry;
  1848. /* Unhook entries from the cache */
  1849. while ((n = rb_first(root_node)) != NULL) {
  1850. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1851. rb_erase(n, root_node);
  1852. list_move(&entry->lru, head);
  1853. }
  1854. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1855. }
  1856. void nfs_access_zap_cache(struct inode *inode)
  1857. {
  1858. LIST_HEAD(head);
  1859. if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
  1860. return;
  1861. /* Remove from global LRU init */
  1862. spin_lock(&nfs_access_lru_lock);
  1863. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1864. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1865. spin_lock(&inode->i_lock);
  1866. __nfs_access_zap_cache(NFS_I(inode), &head);
  1867. spin_unlock(&inode->i_lock);
  1868. spin_unlock(&nfs_access_lru_lock);
  1869. nfs_access_free_list(&head);
  1870. }
  1871. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1872. {
  1873. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1874. struct nfs_access_entry *entry;
  1875. while (n != NULL) {
  1876. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1877. if (cred < entry->cred)
  1878. n = n->rb_left;
  1879. else if (cred > entry->cred)
  1880. n = n->rb_right;
  1881. else
  1882. return entry;
  1883. }
  1884. return NULL;
  1885. }
  1886. static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1887. {
  1888. struct nfs_inode *nfsi = NFS_I(inode);
  1889. struct nfs_access_entry *cache;
  1890. int err = -ENOENT;
  1891. spin_lock(&inode->i_lock);
  1892. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  1893. goto out_zap;
  1894. cache = nfs_access_search_rbtree(inode, cred);
  1895. if (cache == NULL)
  1896. goto out;
  1897. if (!nfs_have_delegated_attributes(inode) &&
  1898. !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
  1899. goto out_stale;
  1900. res->jiffies = cache->jiffies;
  1901. res->cred = cache->cred;
  1902. res->mask = cache->mask;
  1903. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1904. err = 0;
  1905. out:
  1906. spin_unlock(&inode->i_lock);
  1907. return err;
  1908. out_stale:
  1909. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1910. list_del(&cache->lru);
  1911. spin_unlock(&inode->i_lock);
  1912. nfs_access_free_entry(cache);
  1913. return -ENOENT;
  1914. out_zap:
  1915. spin_unlock(&inode->i_lock);
  1916. nfs_access_zap_cache(inode);
  1917. return -ENOENT;
  1918. }
  1919. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1920. {
  1921. struct nfs_inode *nfsi = NFS_I(inode);
  1922. struct rb_root *root_node = &nfsi->access_cache;
  1923. struct rb_node **p = &root_node->rb_node;
  1924. struct rb_node *parent = NULL;
  1925. struct nfs_access_entry *entry;
  1926. spin_lock(&inode->i_lock);
  1927. while (*p != NULL) {
  1928. parent = *p;
  1929. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1930. if (set->cred < entry->cred)
  1931. p = &parent->rb_left;
  1932. else if (set->cred > entry->cred)
  1933. p = &parent->rb_right;
  1934. else
  1935. goto found;
  1936. }
  1937. rb_link_node(&set->rb_node, parent, p);
  1938. rb_insert_color(&set->rb_node, root_node);
  1939. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1940. spin_unlock(&inode->i_lock);
  1941. return;
  1942. found:
  1943. rb_replace_node(parent, &set->rb_node, root_node);
  1944. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1945. list_del(&entry->lru);
  1946. spin_unlock(&inode->i_lock);
  1947. nfs_access_free_entry(entry);
  1948. }
  1949. static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1950. {
  1951. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1952. if (cache == NULL)
  1953. return;
  1954. RB_CLEAR_NODE(&cache->rb_node);
  1955. cache->jiffies = set->jiffies;
  1956. cache->cred = get_rpccred(set->cred);
  1957. cache->mask = set->mask;
  1958. nfs_access_add_rbtree(inode, cache);
  1959. /* Update accounting */
  1960. smp_mb__before_atomic_inc();
  1961. atomic_long_inc(&nfs_access_nr_entries);
  1962. smp_mb__after_atomic_inc();
  1963. /* Add inode to global LRU list */
  1964. if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  1965. spin_lock(&nfs_access_lru_lock);
  1966. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1967. list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
  1968. &nfs_access_lru_list);
  1969. spin_unlock(&nfs_access_lru_lock);
  1970. }
  1971. }
  1972. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1973. {
  1974. struct nfs_access_entry cache;
  1975. int status;
  1976. status = nfs_access_get_cached(inode, cred, &cache);
  1977. if (status == 0)
  1978. goto out;
  1979. /* Be clever: ask server to check for all possible rights */
  1980. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1981. cache.cred = cred;
  1982. cache.jiffies = jiffies;
  1983. status = NFS_PROTO(inode)->access(inode, &cache);
  1984. if (status != 0) {
  1985. if (status == -ESTALE) {
  1986. nfs_zap_caches(inode);
  1987. if (!S_ISDIR(inode->i_mode))
  1988. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  1989. }
  1990. return status;
  1991. }
  1992. nfs_access_add_cache(inode, &cache);
  1993. out:
  1994. if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1995. return 0;
  1996. return -EACCES;
  1997. }
  1998. static int nfs_open_permission_mask(int openflags)
  1999. {
  2000. int mask = 0;
  2001. if ((openflags & O_ACCMODE) != O_WRONLY)
  2002. mask |= MAY_READ;
  2003. if ((openflags & O_ACCMODE) != O_RDONLY)
  2004. mask |= MAY_WRITE;
  2005. if (openflags & __FMODE_EXEC)
  2006. mask |= MAY_EXEC;
  2007. return mask;
  2008. }
  2009. int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  2010. {
  2011. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  2012. }
  2013. int nfs_permission(struct inode *inode, int mask)
  2014. {
  2015. struct rpc_cred *cred;
  2016. int res = 0;
  2017. if (mask & MAY_NOT_BLOCK)
  2018. return -ECHILD;
  2019. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  2020. if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  2021. goto out;
  2022. /* Is this sys_access() ? */
  2023. if (mask & (MAY_ACCESS | MAY_CHDIR))
  2024. goto force_lookup;
  2025. switch (inode->i_mode & S_IFMT) {
  2026. case S_IFLNK:
  2027. goto out;
  2028. case S_IFREG:
  2029. /* NFSv4 has atomic_open... */
  2030. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  2031. && (mask & MAY_OPEN)
  2032. && !(mask & MAY_EXEC))
  2033. goto out;
  2034. break;
  2035. case S_IFDIR:
  2036. /*
  2037. * Optimize away all write operations, since the server
  2038. * will check permissions when we perform the op.
  2039. */
  2040. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  2041. goto out;
  2042. }
  2043. force_lookup:
  2044. if (!NFS_PROTO(inode)->access)
  2045. goto out_notsup;
  2046. cred = rpc_lookup_cred();
  2047. if (!IS_ERR(cred)) {
  2048. res = nfs_do_access(inode, cred, mask);
  2049. put_rpccred(cred);
  2050. } else
  2051. res = PTR_ERR(cred);
  2052. out:
  2053. if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
  2054. res = -EACCES;
  2055. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  2056. inode->i_sb->s_id, inode->i_ino, mask, res);
  2057. return res;
  2058. out_notsup:
  2059. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  2060. if (res == 0)
  2061. res = generic_permission(inode, mask);
  2062. goto out;
  2063. }
  2064. /*
  2065. * Local variables:
  2066. * version-control: t
  2067. * kept-new-versions: 5
  2068. * End:
  2069. */