namei.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726
  1. /*
  2. * linux/fs/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Some corrections by tytso.
  8. */
  9. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  10. * lookup logic.
  11. */
  12. /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/namei.h>
  19. #include <linux/quotaops.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/personality.h>
  24. #include <linux/security.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/mount.h>
  27. #include <linux/audit.h>
  28. #include <linux/capability.h>
  29. #include <linux/file.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/namei.h>
  32. #include <asm/namei.h>
  33. #include <asm/uaccess.h>
  34. #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
  35. /* [Feb-1997 T. Schoebel-Theuer]
  36. * Fundamental changes in the pathname lookup mechanisms (namei)
  37. * were necessary because of omirr. The reason is that omirr needs
  38. * to know the _real_ pathname, not the user-supplied one, in case
  39. * of symlinks (and also when transname replacements occur).
  40. *
  41. * The new code replaces the old recursive symlink resolution with
  42. * an iterative one (in case of non-nested symlink chains). It does
  43. * this with calls to <fs>_follow_link().
  44. * As a side effect, dir_namei(), _namei() and follow_link() are now
  45. * replaced with a single function lookup_dentry() that can handle all
  46. * the special cases of the former code.
  47. *
  48. * With the new dcache, the pathname is stored at each inode, at least as
  49. * long as the refcount of the inode is positive. As a side effect, the
  50. * size of the dcache depends on the inode cache and thus is dynamic.
  51. *
  52. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  53. * resolution to correspond with current state of the code.
  54. *
  55. * Note that the symlink resolution is not *completely* iterative.
  56. * There is still a significant amount of tail- and mid- recursion in
  57. * the algorithm. Also, note that <fs>_readlink() is not used in
  58. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  59. * may return different results than <fs>_follow_link(). Many virtual
  60. * filesystems (including /proc) exhibit this behavior.
  61. */
  62. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  63. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  64. * and the name already exists in form of a symlink, try to create the new
  65. * name indicated by the symlink. The old code always complained that the
  66. * name already exists, due to not following the symlink even if its target
  67. * is nonexistent. The new semantics affects also mknod() and link() when
  68. * the name is a symlink pointing to a non-existant name.
  69. *
  70. * I don't know which semantics is the right one, since I have no access
  71. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  72. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  73. * "old" one. Personally, I think the new semantics is much more logical.
  74. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  75. * file does succeed in both HP-UX and SunOs, but not in Solaris
  76. * and in the old Linux semantics.
  77. */
  78. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  79. * semantics. See the comments in "open_namei" and "do_link" below.
  80. *
  81. * [10-Sep-98 Alan Modra] Another symlink change.
  82. */
  83. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  84. * inside the path - always follow.
  85. * in the last component in creation/removal/renaming - never follow.
  86. * if LOOKUP_FOLLOW passed - follow.
  87. * if the pathname has trailing slashes - follow.
  88. * otherwise - don't follow.
  89. * (applied in that order).
  90. *
  91. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  92. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  93. * During the 2.4 we need to fix the userland stuff depending on it -
  94. * hopefully we will be able to get rid of that wart in 2.5. So far only
  95. * XEmacs seems to be relying on it...
  96. */
  97. /*
  98. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  99. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  100. * any extra contention...
  101. */
  102. /* In order to reduce some races, while at the same time doing additional
  103. * checking and hopefully speeding things up, we copy filenames to the
  104. * kernel data space before using them..
  105. *
  106. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  107. * PATH_MAX includes the nul terminator --RR.
  108. */
  109. static int do_getname(const char __user *filename, char *page)
  110. {
  111. int retval;
  112. unsigned long len = PATH_MAX;
  113. if (!segment_eq(get_fs(), KERNEL_DS)) {
  114. if ((unsigned long) filename >= TASK_SIZE)
  115. return -EFAULT;
  116. if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
  117. len = TASK_SIZE - (unsigned long) filename;
  118. }
  119. retval = strncpy_from_user(page, filename, len);
  120. if (retval > 0) {
  121. if (retval < len)
  122. return 0;
  123. return -ENAMETOOLONG;
  124. } else if (!retval)
  125. retval = -ENOENT;
  126. return retval;
  127. }
  128. char * getname(const char __user * filename)
  129. {
  130. char *tmp, *result;
  131. result = ERR_PTR(-ENOMEM);
  132. tmp = __getname();
  133. if (tmp) {
  134. int retval = do_getname(filename, tmp);
  135. result = tmp;
  136. if (retval < 0) {
  137. __putname(tmp);
  138. result = ERR_PTR(retval);
  139. }
  140. }
  141. audit_getname(result);
  142. return result;
  143. }
  144. #ifdef CONFIG_AUDITSYSCALL
  145. void putname(const char *name)
  146. {
  147. if (unlikely(current->audit_context))
  148. audit_putname(name);
  149. else
  150. __putname(name);
  151. }
  152. EXPORT_SYMBOL(putname);
  153. #endif
  154. /**
  155. * generic_permission - check for access rights on a Posix-like filesystem
  156. * @inode: inode to check access rights for
  157. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  158. * @check_acl: optional callback to check for Posix ACLs
  159. *
  160. * Used to check for read/write/execute permissions on a file.
  161. * We use "fsuid" for this, letting us set arbitrary permissions
  162. * for filesystem access without changing the "normal" uids which
  163. * are used for other things..
  164. */
  165. int generic_permission(struct inode *inode, int mask,
  166. int (*check_acl)(struct inode *inode, int mask))
  167. {
  168. umode_t mode = inode->i_mode;
  169. if (current->fsuid == inode->i_uid)
  170. mode >>= 6;
  171. else {
  172. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  173. int error = check_acl(inode, mask);
  174. if (error == -EACCES)
  175. goto check_capabilities;
  176. else if (error != -EAGAIN)
  177. return error;
  178. }
  179. if (in_group_p(inode->i_gid))
  180. mode >>= 3;
  181. }
  182. /*
  183. * If the DACs are ok we don't need any capability check.
  184. */
  185. if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
  186. return 0;
  187. check_capabilities:
  188. /*
  189. * Read/write DACs are always overridable.
  190. * Executable DACs are overridable if at least one exec bit is set.
  191. */
  192. if (!(mask & MAY_EXEC) ||
  193. (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
  194. if (capable(CAP_DAC_OVERRIDE))
  195. return 0;
  196. /*
  197. * Searching includes executable on directories, else just read.
  198. */
  199. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  200. if (capable(CAP_DAC_READ_SEARCH))
  201. return 0;
  202. return -EACCES;
  203. }
  204. int permission(struct inode *inode, int mask, struct nameidata *nd)
  205. {
  206. int retval, submask;
  207. if (mask & MAY_WRITE) {
  208. umode_t mode = inode->i_mode;
  209. /*
  210. * Nobody gets write access to a read-only fs.
  211. */
  212. if (IS_RDONLY(inode) &&
  213. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  214. return -EROFS;
  215. /*
  216. * Nobody gets write access to an immutable file.
  217. */
  218. if (IS_IMMUTABLE(inode))
  219. return -EACCES;
  220. }
  221. /* Ordinary permission routines do not understand MAY_APPEND. */
  222. submask = mask & ~MAY_APPEND;
  223. if (inode->i_op && inode->i_op->permission)
  224. retval = inode->i_op->permission(inode, submask, nd);
  225. else
  226. retval = generic_permission(inode, submask, NULL);
  227. if (retval)
  228. return retval;
  229. return security_inode_permission(inode, mask, nd);
  230. }
  231. /**
  232. * vfs_permission - check for access rights to a given path
  233. * @nd: lookup result that describes the path
  234. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  235. *
  236. * Used to check for read/write/execute permissions on a path.
  237. * We use "fsuid" for this, letting us set arbitrary permissions
  238. * for filesystem access without changing the "normal" uids which
  239. * are used for other things.
  240. */
  241. int vfs_permission(struct nameidata *nd, int mask)
  242. {
  243. return permission(nd->dentry->d_inode, mask, nd);
  244. }
  245. /**
  246. * file_permission - check for additional access rights to a given file
  247. * @file: file to check access rights for
  248. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  249. *
  250. * Used to check for read/write/execute permissions on an already opened
  251. * file.
  252. *
  253. * Note:
  254. * Do not use this function in new code. All access checks should
  255. * be done using vfs_permission().
  256. */
  257. int file_permission(struct file *file, int mask)
  258. {
  259. return permission(file->f_dentry->d_inode, mask, NULL);
  260. }
  261. /*
  262. * get_write_access() gets write permission for a file.
  263. * put_write_access() releases this write permission.
  264. * This is used for regular files.
  265. * We cannot support write (and maybe mmap read-write shared) accesses and
  266. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  267. * can have the following values:
  268. * 0: no writers, no VM_DENYWRITE mappings
  269. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  270. * > 0: (i_writecount) users are writing to the file.
  271. *
  272. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  273. * except for the cases where we don't hold i_writecount yet. Then we need to
  274. * use {get,deny}_write_access() - these functions check the sign and refuse
  275. * to do the change if sign is wrong. Exclusion between them is provided by
  276. * the inode->i_lock spinlock.
  277. */
  278. int get_write_access(struct inode * inode)
  279. {
  280. spin_lock(&inode->i_lock);
  281. if (atomic_read(&inode->i_writecount) < 0) {
  282. spin_unlock(&inode->i_lock);
  283. return -ETXTBSY;
  284. }
  285. atomic_inc(&inode->i_writecount);
  286. spin_unlock(&inode->i_lock);
  287. return 0;
  288. }
  289. int deny_write_access(struct file * file)
  290. {
  291. struct inode *inode = file->f_dentry->d_inode;
  292. spin_lock(&inode->i_lock);
  293. if (atomic_read(&inode->i_writecount) > 0) {
  294. spin_unlock(&inode->i_lock);
  295. return -ETXTBSY;
  296. }
  297. atomic_dec(&inode->i_writecount);
  298. spin_unlock(&inode->i_lock);
  299. return 0;
  300. }
  301. void path_release(struct nameidata *nd)
  302. {
  303. dput(nd->dentry);
  304. mntput(nd->mnt);
  305. }
  306. /*
  307. * umount() mustn't call path_release()/mntput() as that would clear
  308. * mnt_expiry_mark
  309. */
  310. void path_release_on_umount(struct nameidata *nd)
  311. {
  312. dput(nd->dentry);
  313. mntput_no_expire(nd->mnt);
  314. }
  315. /**
  316. * release_open_intent - free up open intent resources
  317. * @nd: pointer to nameidata
  318. */
  319. void release_open_intent(struct nameidata *nd)
  320. {
  321. if (nd->intent.open.file->f_dentry == NULL)
  322. put_filp(nd->intent.open.file);
  323. else
  324. fput(nd->intent.open.file);
  325. }
  326. /*
  327. * Internal lookup() using the new generic dcache.
  328. * SMP-safe
  329. */
  330. static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  331. {
  332. struct dentry * dentry = __d_lookup(parent, name);
  333. /* lockess __d_lookup may fail due to concurrent d_move()
  334. * in some unrelated directory, so try with d_lookup
  335. */
  336. if (!dentry)
  337. dentry = d_lookup(parent, name);
  338. if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
  339. if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
  340. dput(dentry);
  341. dentry = NULL;
  342. }
  343. }
  344. return dentry;
  345. }
  346. /*
  347. * Short-cut version of permission(), for calling by
  348. * path_walk(), when dcache lock is held. Combines parts
  349. * of permission() and generic_permission(), and tests ONLY for
  350. * MAY_EXEC permission.
  351. *
  352. * If appropriate, check DAC only. If not appropriate, or
  353. * short-cut DAC fails, then call permission() to do more
  354. * complete permission check.
  355. */
  356. static int exec_permission_lite(struct inode *inode,
  357. struct nameidata *nd)
  358. {
  359. umode_t mode = inode->i_mode;
  360. if (inode->i_op && inode->i_op->permission)
  361. return -EAGAIN;
  362. if (current->fsuid == inode->i_uid)
  363. mode >>= 6;
  364. else if (in_group_p(inode->i_gid))
  365. mode >>= 3;
  366. if (mode & MAY_EXEC)
  367. goto ok;
  368. if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
  369. goto ok;
  370. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
  371. goto ok;
  372. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
  373. goto ok;
  374. return -EACCES;
  375. ok:
  376. return security_inode_permission(inode, MAY_EXEC, nd);
  377. }
  378. /*
  379. * This is called when everything else fails, and we actually have
  380. * to go to the low-level filesystem to find out what we should do..
  381. *
  382. * We get the directory semaphore, and after getting that we also
  383. * make sure that nobody added the entry to the dcache in the meantime..
  384. * SMP-safe
  385. */
  386. static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  387. {
  388. struct dentry * result;
  389. struct inode *dir = parent->d_inode;
  390. mutex_lock(&dir->i_mutex);
  391. /*
  392. * First re-do the cached lookup just in case it was created
  393. * while we waited for the directory semaphore..
  394. *
  395. * FIXME! This could use version numbering or similar to
  396. * avoid unnecessary cache lookups.
  397. *
  398. * The "dcache_lock" is purely to protect the RCU list walker
  399. * from concurrent renames at this point (we mustn't get false
  400. * negatives from the RCU list walk here, unlike the optimistic
  401. * fast walk).
  402. *
  403. * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
  404. */
  405. result = d_lookup(parent, name);
  406. if (!result) {
  407. struct dentry * dentry = d_alloc(parent, name);
  408. result = ERR_PTR(-ENOMEM);
  409. if (dentry) {
  410. result = dir->i_op->lookup(dir, dentry, nd);
  411. if (result)
  412. dput(dentry);
  413. else
  414. result = dentry;
  415. }
  416. mutex_unlock(&dir->i_mutex);
  417. return result;
  418. }
  419. /*
  420. * Uhhuh! Nasty case: the cache was re-populated while
  421. * we waited on the semaphore. Need to revalidate.
  422. */
  423. mutex_unlock(&dir->i_mutex);
  424. if (result->d_op && result->d_op->d_revalidate) {
  425. if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
  426. dput(result);
  427. result = ERR_PTR(-ENOENT);
  428. }
  429. }
  430. return result;
  431. }
  432. static int __emul_lookup_dentry(const char *, struct nameidata *);
  433. /* SMP-safe */
  434. static __always_inline int
  435. walk_init_root(const char *name, struct nameidata *nd)
  436. {
  437. read_lock(&current->fs->lock);
  438. if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  439. nd->mnt = mntget(current->fs->altrootmnt);
  440. nd->dentry = dget(current->fs->altroot);
  441. read_unlock(&current->fs->lock);
  442. if (__emul_lookup_dentry(name,nd))
  443. return 0;
  444. read_lock(&current->fs->lock);
  445. }
  446. nd->mnt = mntget(current->fs->rootmnt);
  447. nd->dentry = dget(current->fs->root);
  448. read_unlock(&current->fs->lock);
  449. return 1;
  450. }
  451. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  452. {
  453. int res = 0;
  454. char *name;
  455. if (IS_ERR(link))
  456. goto fail;
  457. if (*link == '/') {
  458. path_release(nd);
  459. if (!walk_init_root(link, nd))
  460. /* weird __emul_prefix() stuff did it */
  461. goto out;
  462. }
  463. res = link_path_walk(link, nd);
  464. out:
  465. if (nd->depth || res || nd->last_type!=LAST_NORM)
  466. return res;
  467. /*
  468. * If it is an iterative symlinks resolution in open_namei() we
  469. * have to copy the last component. And all that crap because of
  470. * bloody create() on broken symlinks. Furrfu...
  471. */
  472. name = __getname();
  473. if (unlikely(!name)) {
  474. path_release(nd);
  475. return -ENOMEM;
  476. }
  477. strcpy(name, nd->last.name);
  478. nd->last.name = name;
  479. return 0;
  480. fail:
  481. path_release(nd);
  482. return PTR_ERR(link);
  483. }
  484. struct path {
  485. struct vfsmount *mnt;
  486. struct dentry *dentry;
  487. };
  488. static inline void dput_path(struct path *path, struct nameidata *nd)
  489. {
  490. dput(path->dentry);
  491. if (path->mnt != nd->mnt)
  492. mntput(path->mnt);
  493. }
  494. static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
  495. {
  496. dput(nd->dentry);
  497. if (nd->mnt != path->mnt)
  498. mntput(nd->mnt);
  499. nd->mnt = path->mnt;
  500. nd->dentry = path->dentry;
  501. }
  502. static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
  503. {
  504. int error;
  505. void *cookie;
  506. struct dentry *dentry = path->dentry;
  507. touch_atime(path->mnt, dentry);
  508. nd_set_link(nd, NULL);
  509. if (path->mnt != nd->mnt) {
  510. path_to_nameidata(path, nd);
  511. dget(dentry);
  512. }
  513. mntget(path->mnt);
  514. cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
  515. error = PTR_ERR(cookie);
  516. if (!IS_ERR(cookie)) {
  517. char *s = nd_get_link(nd);
  518. error = 0;
  519. if (s)
  520. error = __vfs_follow_link(nd, s);
  521. if (dentry->d_inode->i_op->put_link)
  522. dentry->d_inode->i_op->put_link(dentry, nd, cookie);
  523. }
  524. dput(dentry);
  525. mntput(path->mnt);
  526. return error;
  527. }
  528. /*
  529. * This limits recursive symlink follows to 8, while
  530. * limiting consecutive symlinks to 40.
  531. *
  532. * Without that kind of total limit, nasty chains of consecutive
  533. * symlinks can cause almost arbitrarily long lookups.
  534. */
  535. static inline int do_follow_link(struct path *path, struct nameidata *nd)
  536. {
  537. int err = -ELOOP;
  538. if (current->link_count >= MAX_NESTED_LINKS)
  539. goto loop;
  540. if (current->total_link_count >= 40)
  541. goto loop;
  542. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  543. cond_resched();
  544. err = security_inode_follow_link(path->dentry, nd);
  545. if (err)
  546. goto loop;
  547. current->link_count++;
  548. current->total_link_count++;
  549. nd->depth++;
  550. err = __do_follow_link(path, nd);
  551. current->link_count--;
  552. nd->depth--;
  553. return err;
  554. loop:
  555. dput_path(path, nd);
  556. path_release(nd);
  557. return err;
  558. }
  559. int follow_up(struct vfsmount **mnt, struct dentry **dentry)
  560. {
  561. struct vfsmount *parent;
  562. struct dentry *mountpoint;
  563. spin_lock(&vfsmount_lock);
  564. parent=(*mnt)->mnt_parent;
  565. if (parent == *mnt) {
  566. spin_unlock(&vfsmount_lock);
  567. return 0;
  568. }
  569. mntget(parent);
  570. mountpoint=dget((*mnt)->mnt_mountpoint);
  571. spin_unlock(&vfsmount_lock);
  572. dput(*dentry);
  573. *dentry = mountpoint;
  574. mntput(*mnt);
  575. *mnt = parent;
  576. return 1;
  577. }
  578. /* no need for dcache_lock, as serialization is taken care in
  579. * namespace.c
  580. */
  581. static int __follow_mount(struct path *path)
  582. {
  583. int res = 0;
  584. while (d_mountpoint(path->dentry)) {
  585. struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
  586. if (!mounted)
  587. break;
  588. dput(path->dentry);
  589. if (res)
  590. mntput(path->mnt);
  591. path->mnt = mounted;
  592. path->dentry = dget(mounted->mnt_root);
  593. res = 1;
  594. }
  595. return res;
  596. }
  597. static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
  598. {
  599. while (d_mountpoint(*dentry)) {
  600. struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
  601. if (!mounted)
  602. break;
  603. dput(*dentry);
  604. mntput(*mnt);
  605. *mnt = mounted;
  606. *dentry = dget(mounted->mnt_root);
  607. }
  608. }
  609. /* no need for dcache_lock, as serialization is taken care in
  610. * namespace.c
  611. */
  612. int follow_down(struct vfsmount **mnt, struct dentry **dentry)
  613. {
  614. struct vfsmount *mounted;
  615. mounted = lookup_mnt(*mnt, *dentry);
  616. if (mounted) {
  617. dput(*dentry);
  618. mntput(*mnt);
  619. *mnt = mounted;
  620. *dentry = dget(mounted->mnt_root);
  621. return 1;
  622. }
  623. return 0;
  624. }
  625. static __always_inline void follow_dotdot(struct nameidata *nd)
  626. {
  627. while(1) {
  628. struct vfsmount *parent;
  629. struct dentry *old = nd->dentry;
  630. read_lock(&current->fs->lock);
  631. if (nd->dentry == current->fs->root &&
  632. nd->mnt == current->fs->rootmnt) {
  633. read_unlock(&current->fs->lock);
  634. break;
  635. }
  636. read_unlock(&current->fs->lock);
  637. spin_lock(&dcache_lock);
  638. if (nd->dentry != nd->mnt->mnt_root) {
  639. nd->dentry = dget(nd->dentry->d_parent);
  640. spin_unlock(&dcache_lock);
  641. dput(old);
  642. break;
  643. }
  644. spin_unlock(&dcache_lock);
  645. spin_lock(&vfsmount_lock);
  646. parent = nd->mnt->mnt_parent;
  647. if (parent == nd->mnt) {
  648. spin_unlock(&vfsmount_lock);
  649. break;
  650. }
  651. mntget(parent);
  652. nd->dentry = dget(nd->mnt->mnt_mountpoint);
  653. spin_unlock(&vfsmount_lock);
  654. dput(old);
  655. mntput(nd->mnt);
  656. nd->mnt = parent;
  657. }
  658. follow_mount(&nd->mnt, &nd->dentry);
  659. }
  660. /*
  661. * It's more convoluted than I'd like it to be, but... it's still fairly
  662. * small and for now I'd prefer to have fast path as straight as possible.
  663. * It _is_ time-critical.
  664. */
  665. static int do_lookup(struct nameidata *nd, struct qstr *name,
  666. struct path *path)
  667. {
  668. struct vfsmount *mnt = nd->mnt;
  669. struct dentry *dentry = __d_lookup(nd->dentry, name);
  670. if (!dentry)
  671. goto need_lookup;
  672. if (dentry->d_op && dentry->d_op->d_revalidate)
  673. goto need_revalidate;
  674. done:
  675. path->mnt = mnt;
  676. path->dentry = dentry;
  677. __follow_mount(path);
  678. return 0;
  679. need_lookup:
  680. dentry = real_lookup(nd->dentry, name, nd);
  681. if (IS_ERR(dentry))
  682. goto fail;
  683. goto done;
  684. need_revalidate:
  685. if (dentry->d_op->d_revalidate(dentry, nd))
  686. goto done;
  687. if (d_invalidate(dentry))
  688. goto done;
  689. dput(dentry);
  690. goto need_lookup;
  691. fail:
  692. return PTR_ERR(dentry);
  693. }
  694. /*
  695. * Name resolution.
  696. * This is the basic name resolution function, turning a pathname into
  697. * the final dentry. We expect 'base' to be positive and a directory.
  698. *
  699. * Returns 0 and nd will have valid dentry and mnt on success.
  700. * Returns error and drops reference to input namei data on failure.
  701. */
  702. static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
  703. {
  704. struct path next;
  705. struct inode *inode;
  706. int err;
  707. unsigned int lookup_flags = nd->flags;
  708. while (*name=='/')
  709. name++;
  710. if (!*name)
  711. goto return_reval;
  712. inode = nd->dentry->d_inode;
  713. if (nd->depth)
  714. lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
  715. /* At this point we know we have a real path component. */
  716. for(;;) {
  717. unsigned long hash;
  718. struct qstr this;
  719. unsigned int c;
  720. nd->flags |= LOOKUP_CONTINUE;
  721. err = exec_permission_lite(inode, nd);
  722. if (err == -EAGAIN)
  723. err = vfs_permission(nd, MAY_EXEC);
  724. if (err)
  725. break;
  726. this.name = name;
  727. c = *(const unsigned char *)name;
  728. hash = init_name_hash();
  729. do {
  730. name++;
  731. hash = partial_name_hash(c, hash);
  732. c = *(const unsigned char *)name;
  733. } while (c && (c != '/'));
  734. this.len = name - (const char *) this.name;
  735. this.hash = end_name_hash(hash);
  736. /* remove trailing slashes? */
  737. if (!c)
  738. goto last_component;
  739. while (*++name == '/');
  740. if (!*name)
  741. goto last_with_slashes;
  742. /*
  743. * "." and ".." are special - ".." especially so because it has
  744. * to be able to know about the current root directory and
  745. * parent relationships.
  746. */
  747. if (this.name[0] == '.') switch (this.len) {
  748. default:
  749. break;
  750. case 2:
  751. if (this.name[1] != '.')
  752. break;
  753. follow_dotdot(nd);
  754. inode = nd->dentry->d_inode;
  755. /* fallthrough */
  756. case 1:
  757. continue;
  758. }
  759. /*
  760. * See if the low-level filesystem might want
  761. * to use its own hash..
  762. */
  763. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  764. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  765. if (err < 0)
  766. break;
  767. }
  768. /* This does the actual lookups.. */
  769. err = do_lookup(nd, &this, &next);
  770. if (err)
  771. break;
  772. err = -ENOENT;
  773. inode = next.dentry->d_inode;
  774. if (!inode)
  775. goto out_dput;
  776. err = -ENOTDIR;
  777. if (!inode->i_op)
  778. goto out_dput;
  779. if (inode->i_op->follow_link) {
  780. err = do_follow_link(&next, nd);
  781. if (err)
  782. goto return_err;
  783. err = -ENOENT;
  784. inode = nd->dentry->d_inode;
  785. if (!inode)
  786. break;
  787. err = -ENOTDIR;
  788. if (!inode->i_op)
  789. break;
  790. } else
  791. path_to_nameidata(&next, nd);
  792. err = -ENOTDIR;
  793. if (!inode->i_op->lookup)
  794. break;
  795. continue;
  796. /* here ends the main loop */
  797. last_with_slashes:
  798. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  799. last_component:
  800. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  801. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  802. if (lookup_flags & LOOKUP_PARENT)
  803. goto lookup_parent;
  804. if (this.name[0] == '.') switch (this.len) {
  805. default:
  806. break;
  807. case 2:
  808. if (this.name[1] != '.')
  809. break;
  810. follow_dotdot(nd);
  811. inode = nd->dentry->d_inode;
  812. /* fallthrough */
  813. case 1:
  814. goto return_reval;
  815. }
  816. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  817. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  818. if (err < 0)
  819. break;
  820. }
  821. err = do_lookup(nd, &this, &next);
  822. if (err)
  823. break;
  824. inode = next.dentry->d_inode;
  825. if ((lookup_flags & LOOKUP_FOLLOW)
  826. && inode && inode->i_op && inode->i_op->follow_link) {
  827. err = do_follow_link(&next, nd);
  828. if (err)
  829. goto return_err;
  830. inode = nd->dentry->d_inode;
  831. } else
  832. path_to_nameidata(&next, nd);
  833. err = -ENOENT;
  834. if (!inode)
  835. break;
  836. if (lookup_flags & LOOKUP_DIRECTORY) {
  837. err = -ENOTDIR;
  838. if (!inode->i_op || !inode->i_op->lookup)
  839. break;
  840. }
  841. goto return_base;
  842. lookup_parent:
  843. nd->last = this;
  844. nd->last_type = LAST_NORM;
  845. if (this.name[0] != '.')
  846. goto return_base;
  847. if (this.len == 1)
  848. nd->last_type = LAST_DOT;
  849. else if (this.len == 2 && this.name[1] == '.')
  850. nd->last_type = LAST_DOTDOT;
  851. else
  852. goto return_base;
  853. return_reval:
  854. /*
  855. * We bypassed the ordinary revalidation routines.
  856. * We may need to check the cached dentry for staleness.
  857. */
  858. if (nd->dentry && nd->dentry->d_sb &&
  859. (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
  860. err = -ESTALE;
  861. /* Note: we do not d_invalidate() */
  862. if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
  863. break;
  864. }
  865. return_base:
  866. return 0;
  867. out_dput:
  868. dput_path(&next, nd);
  869. break;
  870. }
  871. path_release(nd);
  872. return_err:
  873. return err;
  874. }
  875. /*
  876. * Wrapper to retry pathname resolution whenever the underlying
  877. * file system returns an ESTALE.
  878. *
  879. * Retry the whole path once, forcing real lookup requests
  880. * instead of relying on the dcache.
  881. */
  882. int fastcall link_path_walk(const char *name, struct nameidata *nd)
  883. {
  884. struct nameidata save = *nd;
  885. int result;
  886. /* make sure the stuff we saved doesn't go away */
  887. dget(save.dentry);
  888. mntget(save.mnt);
  889. result = __link_path_walk(name, nd);
  890. if (result == -ESTALE) {
  891. *nd = save;
  892. dget(nd->dentry);
  893. mntget(nd->mnt);
  894. nd->flags |= LOOKUP_REVAL;
  895. result = __link_path_walk(name, nd);
  896. }
  897. dput(save.dentry);
  898. mntput(save.mnt);
  899. return result;
  900. }
  901. int fastcall path_walk(const char * name, struct nameidata *nd)
  902. {
  903. current->total_link_count = 0;
  904. return link_path_walk(name, nd);
  905. }
  906. /*
  907. * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
  908. * everything is done. Returns 0 and drops input nd, if lookup failed;
  909. */
  910. static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
  911. {
  912. if (path_walk(name, nd))
  913. return 0; /* something went wrong... */
  914. if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
  915. struct dentry *old_dentry = nd->dentry;
  916. struct vfsmount *old_mnt = nd->mnt;
  917. struct qstr last = nd->last;
  918. int last_type = nd->last_type;
  919. /*
  920. * NAME was not found in alternate root or it's a directory. Try to find
  921. * it in the normal root:
  922. */
  923. nd->last_type = LAST_ROOT;
  924. read_lock(&current->fs->lock);
  925. nd->mnt = mntget(current->fs->rootmnt);
  926. nd->dentry = dget(current->fs->root);
  927. read_unlock(&current->fs->lock);
  928. if (path_walk(name, nd) == 0) {
  929. if (nd->dentry->d_inode) {
  930. dput(old_dentry);
  931. mntput(old_mnt);
  932. return 1;
  933. }
  934. path_release(nd);
  935. }
  936. nd->dentry = old_dentry;
  937. nd->mnt = old_mnt;
  938. nd->last = last;
  939. nd->last_type = last_type;
  940. }
  941. return 1;
  942. }
  943. void set_fs_altroot(void)
  944. {
  945. char *emul = __emul_prefix();
  946. struct nameidata nd;
  947. struct vfsmount *mnt = NULL, *oldmnt;
  948. struct dentry *dentry = NULL, *olddentry;
  949. int err;
  950. if (!emul)
  951. goto set_it;
  952. err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
  953. if (!err) {
  954. mnt = nd.mnt;
  955. dentry = nd.dentry;
  956. }
  957. set_it:
  958. write_lock(&current->fs->lock);
  959. oldmnt = current->fs->altrootmnt;
  960. olddentry = current->fs->altroot;
  961. current->fs->altrootmnt = mnt;
  962. current->fs->altroot = dentry;
  963. write_unlock(&current->fs->lock);
  964. if (olddentry) {
  965. dput(olddentry);
  966. mntput(oldmnt);
  967. }
  968. }
  969. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  970. static int fastcall do_path_lookup(int dfd, const char *name,
  971. unsigned int flags, struct nameidata *nd)
  972. {
  973. int retval = 0;
  974. int fput_needed;
  975. struct file *file;
  976. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  977. nd->flags = flags;
  978. nd->depth = 0;
  979. read_lock(&current->fs->lock);
  980. if (*name=='/') {
  981. if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  982. nd->mnt = mntget(current->fs->altrootmnt);
  983. nd->dentry = dget(current->fs->altroot);
  984. read_unlock(&current->fs->lock);
  985. if (__emul_lookup_dentry(name,nd))
  986. goto out; /* found in altroot */
  987. read_lock(&current->fs->lock);
  988. }
  989. nd->mnt = mntget(current->fs->rootmnt);
  990. nd->dentry = dget(current->fs->root);
  991. } else if (dfd == AT_FDCWD) {
  992. nd->mnt = mntget(current->fs->pwdmnt);
  993. nd->dentry = dget(current->fs->pwd);
  994. } else {
  995. struct dentry *dentry;
  996. file = fget_light(dfd, &fput_needed);
  997. retval = -EBADF;
  998. if (!file)
  999. goto unlock_fail;
  1000. dentry = file->f_dentry;
  1001. retval = -ENOTDIR;
  1002. if (!S_ISDIR(dentry->d_inode->i_mode))
  1003. goto fput_unlock_fail;
  1004. retval = file_permission(file, MAY_EXEC);
  1005. if (retval)
  1006. goto fput_unlock_fail;
  1007. nd->mnt = mntget(file->f_vfsmnt);
  1008. nd->dentry = dget(dentry);
  1009. fput_light(file, fput_needed);
  1010. }
  1011. read_unlock(&current->fs->lock);
  1012. current->total_link_count = 0;
  1013. retval = link_path_walk(name, nd);
  1014. out:
  1015. if (likely(retval == 0)) {
  1016. if (unlikely(current->audit_context && nd && nd->dentry &&
  1017. nd->dentry->d_inode))
  1018. audit_inode(name, nd->dentry->d_inode, flags);
  1019. }
  1020. return retval;
  1021. fput_unlock_fail:
  1022. fput_light(file, fput_needed);
  1023. unlock_fail:
  1024. read_unlock(&current->fs->lock);
  1025. return retval;
  1026. }
  1027. int fastcall path_lookup(const char *name, unsigned int flags,
  1028. struct nameidata *nd)
  1029. {
  1030. return do_path_lookup(AT_FDCWD, name, flags, nd);
  1031. }
  1032. static int __path_lookup_intent_open(int dfd, const char *name,
  1033. unsigned int lookup_flags, struct nameidata *nd,
  1034. int open_flags, int create_mode)
  1035. {
  1036. struct file *filp = get_empty_filp();
  1037. int err;
  1038. if (filp == NULL)
  1039. return -ENFILE;
  1040. nd->intent.open.file = filp;
  1041. nd->intent.open.flags = open_flags;
  1042. nd->intent.open.create_mode = create_mode;
  1043. err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
  1044. if (IS_ERR(nd->intent.open.file)) {
  1045. if (err == 0) {
  1046. err = PTR_ERR(nd->intent.open.file);
  1047. path_release(nd);
  1048. }
  1049. } else if (err != 0)
  1050. release_open_intent(nd);
  1051. return err;
  1052. }
  1053. /**
  1054. * path_lookup_open - lookup a file path with open intent
  1055. * @dfd: the directory to use as base, or AT_FDCWD
  1056. * @name: pointer to file name
  1057. * @lookup_flags: lookup intent flags
  1058. * @nd: pointer to nameidata
  1059. * @open_flags: open intent flags
  1060. */
  1061. int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
  1062. struct nameidata *nd, int open_flags)
  1063. {
  1064. return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
  1065. open_flags, 0);
  1066. }
  1067. /**
  1068. * path_lookup_create - lookup a file path with open + create intent
  1069. * @dfd: the directory to use as base, or AT_FDCWD
  1070. * @name: pointer to file name
  1071. * @lookup_flags: lookup intent flags
  1072. * @nd: pointer to nameidata
  1073. * @open_flags: open intent flags
  1074. * @create_mode: create intent flags
  1075. */
  1076. static int path_lookup_create(int dfd, const char *name,
  1077. unsigned int lookup_flags, struct nameidata *nd,
  1078. int open_flags, int create_mode)
  1079. {
  1080. return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
  1081. nd, open_flags, create_mode);
  1082. }
  1083. int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
  1084. struct nameidata *nd, int open_flags)
  1085. {
  1086. char *tmp = getname(name);
  1087. int err = PTR_ERR(tmp);
  1088. if (!IS_ERR(tmp)) {
  1089. err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
  1090. putname(tmp);
  1091. }
  1092. return err;
  1093. }
  1094. /*
  1095. * Restricted form of lookup. Doesn't follow links, single-component only,
  1096. * needs parent already locked. Doesn't follow mounts.
  1097. * SMP-safe.
  1098. */
  1099. static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
  1100. {
  1101. struct dentry * dentry;
  1102. struct inode *inode;
  1103. int err;
  1104. inode = base->d_inode;
  1105. err = permission(inode, MAY_EXEC, nd);
  1106. dentry = ERR_PTR(err);
  1107. if (err)
  1108. goto out;
  1109. /*
  1110. * See if the low-level filesystem might want
  1111. * to use its own hash..
  1112. */
  1113. if (base->d_op && base->d_op->d_hash) {
  1114. err = base->d_op->d_hash(base, name);
  1115. dentry = ERR_PTR(err);
  1116. if (err < 0)
  1117. goto out;
  1118. }
  1119. dentry = cached_lookup(base, name, nd);
  1120. if (!dentry) {
  1121. struct dentry *new = d_alloc(base, name);
  1122. dentry = ERR_PTR(-ENOMEM);
  1123. if (!new)
  1124. goto out;
  1125. dentry = inode->i_op->lookup(inode, new, nd);
  1126. if (!dentry)
  1127. dentry = new;
  1128. else
  1129. dput(new);
  1130. }
  1131. out:
  1132. return dentry;
  1133. }
  1134. static struct dentry *lookup_hash(struct nameidata *nd)
  1135. {
  1136. return __lookup_hash(&nd->last, nd->dentry, nd);
  1137. }
  1138. /* SMP-safe */
  1139. struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
  1140. {
  1141. unsigned long hash;
  1142. struct qstr this;
  1143. unsigned int c;
  1144. this.name = name;
  1145. this.len = len;
  1146. if (!len)
  1147. goto access;
  1148. hash = init_name_hash();
  1149. while (len--) {
  1150. c = *(const unsigned char *)name++;
  1151. if (c == '/' || c == '\0')
  1152. goto access;
  1153. hash = partial_name_hash(c, hash);
  1154. }
  1155. this.hash = end_name_hash(hash);
  1156. return __lookup_hash(&this, base, NULL);
  1157. access:
  1158. return ERR_PTR(-EACCES);
  1159. }
  1160. /*
  1161. * namei()
  1162. *
  1163. * is used by most simple commands to get the inode of a specified name.
  1164. * Open, link etc use their own routines, but this is enough for things
  1165. * like 'chmod' etc.
  1166. *
  1167. * namei exists in two versions: namei/lnamei. The only difference is
  1168. * that namei follows links, while lnamei does not.
  1169. * SMP-safe
  1170. */
  1171. int fastcall __user_walk_fd(int dfd, const char __user *name, unsigned flags,
  1172. struct nameidata *nd)
  1173. {
  1174. char *tmp = getname(name);
  1175. int err = PTR_ERR(tmp);
  1176. if (!IS_ERR(tmp)) {
  1177. err = do_path_lookup(dfd, tmp, flags, nd);
  1178. putname(tmp);
  1179. }
  1180. return err;
  1181. }
  1182. int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
  1183. {
  1184. return __user_walk_fd(AT_FDCWD, name, flags, nd);
  1185. }
  1186. /*
  1187. * It's inline, so penalty for filesystems that don't use sticky bit is
  1188. * minimal.
  1189. */
  1190. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1191. {
  1192. if (!(dir->i_mode & S_ISVTX))
  1193. return 0;
  1194. if (inode->i_uid == current->fsuid)
  1195. return 0;
  1196. if (dir->i_uid == current->fsuid)
  1197. return 0;
  1198. return !capable(CAP_FOWNER);
  1199. }
  1200. /*
  1201. * Check whether we can remove a link victim from directory dir, check
  1202. * whether the type of victim is right.
  1203. * 1. We can't do it if dir is read-only (done in permission())
  1204. * 2. We should have write and exec permissions on dir
  1205. * 3. We can't remove anything from append-only dir
  1206. * 4. We can't do anything with immutable dir (done in permission())
  1207. * 5. If the sticky bit on dir is set we should either
  1208. * a. be owner of dir, or
  1209. * b. be owner of victim, or
  1210. * c. have CAP_FOWNER capability
  1211. * 6. If the victim is append-only or immutable we can't do antyhing with
  1212. * links pointing to it.
  1213. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1214. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1215. * 9. We can't remove a root or mountpoint.
  1216. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1217. * nfs_async_unlink().
  1218. */
  1219. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1220. {
  1221. int error;
  1222. if (!victim->d_inode)
  1223. return -ENOENT;
  1224. BUG_ON(victim->d_parent->d_inode != dir);
  1225. audit_inode_child(victim->d_name.name, victim->d_inode, dir->i_ino);
  1226. error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
  1227. if (error)
  1228. return error;
  1229. if (IS_APPEND(dir))
  1230. return -EPERM;
  1231. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1232. IS_IMMUTABLE(victim->d_inode))
  1233. return -EPERM;
  1234. if (isdir) {
  1235. if (!S_ISDIR(victim->d_inode->i_mode))
  1236. return -ENOTDIR;
  1237. if (IS_ROOT(victim))
  1238. return -EBUSY;
  1239. } else if (S_ISDIR(victim->d_inode->i_mode))
  1240. return -EISDIR;
  1241. if (IS_DEADDIR(dir))
  1242. return -ENOENT;
  1243. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1244. return -EBUSY;
  1245. return 0;
  1246. }
  1247. /* Check whether we can create an object with dentry child in directory
  1248. * dir.
  1249. * 1. We can't do it if child already exists (open has special treatment for
  1250. * this case, but since we are inlined it's OK)
  1251. * 2. We can't do it if dir is read-only (done in permission())
  1252. * 3. We should have write and exec permissions on dir
  1253. * 4. We can't do it if dir is immutable (done in permission())
  1254. */
  1255. static inline int may_create(struct inode *dir, struct dentry *child,
  1256. struct nameidata *nd)
  1257. {
  1258. if (child->d_inode)
  1259. return -EEXIST;
  1260. if (IS_DEADDIR(dir))
  1261. return -ENOENT;
  1262. return permission(dir,MAY_WRITE | MAY_EXEC, nd);
  1263. }
  1264. /*
  1265. * O_DIRECTORY translates into forcing a directory lookup.
  1266. */
  1267. static inline int lookup_flags(unsigned int f)
  1268. {
  1269. unsigned long retval = LOOKUP_FOLLOW;
  1270. if (f & O_NOFOLLOW)
  1271. retval &= ~LOOKUP_FOLLOW;
  1272. if (f & O_DIRECTORY)
  1273. retval |= LOOKUP_DIRECTORY;
  1274. return retval;
  1275. }
  1276. /*
  1277. * p1 and p2 should be directories on the same fs.
  1278. */
  1279. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1280. {
  1281. struct dentry *p;
  1282. if (p1 == p2) {
  1283. mutex_lock(&p1->d_inode->i_mutex);
  1284. return NULL;
  1285. }
  1286. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1287. for (p = p1; p->d_parent != p; p = p->d_parent) {
  1288. if (p->d_parent == p2) {
  1289. mutex_lock(&p2->d_inode->i_mutex);
  1290. mutex_lock(&p1->d_inode->i_mutex);
  1291. return p;
  1292. }
  1293. }
  1294. for (p = p2; p->d_parent != p; p = p->d_parent) {
  1295. if (p->d_parent == p1) {
  1296. mutex_lock(&p1->d_inode->i_mutex);
  1297. mutex_lock(&p2->d_inode->i_mutex);
  1298. return p;
  1299. }
  1300. }
  1301. mutex_lock(&p1->d_inode->i_mutex);
  1302. mutex_lock(&p2->d_inode->i_mutex);
  1303. return NULL;
  1304. }
  1305. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1306. {
  1307. mutex_unlock(&p1->d_inode->i_mutex);
  1308. if (p1 != p2) {
  1309. mutex_unlock(&p2->d_inode->i_mutex);
  1310. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1311. }
  1312. }
  1313. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1314. struct nameidata *nd)
  1315. {
  1316. int error = may_create(dir, dentry, nd);
  1317. if (error)
  1318. return error;
  1319. if (!dir->i_op || !dir->i_op->create)
  1320. return -EACCES; /* shouldn't it be ENOSYS? */
  1321. mode &= S_IALLUGO;
  1322. mode |= S_IFREG;
  1323. error = security_inode_create(dir, dentry, mode);
  1324. if (error)
  1325. return error;
  1326. DQUOT_INIT(dir);
  1327. error = dir->i_op->create(dir, dentry, mode, nd);
  1328. if (!error)
  1329. fsnotify_create(dir, dentry);
  1330. return error;
  1331. }
  1332. int may_open(struct nameidata *nd, int acc_mode, int flag)
  1333. {
  1334. struct dentry *dentry = nd->dentry;
  1335. struct inode *inode = dentry->d_inode;
  1336. int error;
  1337. if (!inode)
  1338. return -ENOENT;
  1339. if (S_ISLNK(inode->i_mode))
  1340. return -ELOOP;
  1341. if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
  1342. return -EISDIR;
  1343. error = vfs_permission(nd, acc_mode);
  1344. if (error)
  1345. return error;
  1346. /*
  1347. * FIFO's, sockets and device files are special: they don't
  1348. * actually live on the filesystem itself, and as such you
  1349. * can write to them even if the filesystem is read-only.
  1350. */
  1351. if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1352. flag &= ~O_TRUNC;
  1353. } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
  1354. if (nd->mnt->mnt_flags & MNT_NODEV)
  1355. return -EACCES;
  1356. flag &= ~O_TRUNC;
  1357. } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
  1358. return -EROFS;
  1359. /*
  1360. * An append-only file must be opened in append mode for writing.
  1361. */
  1362. if (IS_APPEND(inode)) {
  1363. if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
  1364. return -EPERM;
  1365. if (flag & O_TRUNC)
  1366. return -EPERM;
  1367. }
  1368. /* O_NOATIME can only be set by the owner or superuser */
  1369. if (flag & O_NOATIME)
  1370. if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
  1371. return -EPERM;
  1372. /*
  1373. * Ensure there are no outstanding leases on the file.
  1374. */
  1375. error = break_lease(inode, flag);
  1376. if (error)
  1377. return error;
  1378. if (flag & O_TRUNC) {
  1379. error = get_write_access(inode);
  1380. if (error)
  1381. return error;
  1382. /*
  1383. * Refuse to truncate files with mandatory locks held on them.
  1384. */
  1385. error = locks_verify_locked(inode);
  1386. if (!error) {
  1387. DQUOT_INIT(inode);
  1388. error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL);
  1389. }
  1390. put_write_access(inode);
  1391. if (error)
  1392. return error;
  1393. } else
  1394. if (flag & FMODE_WRITE)
  1395. DQUOT_INIT(inode);
  1396. return 0;
  1397. }
  1398. /*
  1399. * open_namei()
  1400. *
  1401. * namei for open - this is in fact almost the whole open-routine.
  1402. *
  1403. * Note that the low bits of "flag" aren't the same as in the open
  1404. * system call - they are 00 - no permissions needed
  1405. * 01 - read permission needed
  1406. * 10 - write permission needed
  1407. * 11 - read/write permissions needed
  1408. * which is a lot more logical, and also allows the "no perm" needed
  1409. * for symlinks (where the permissions are checked later).
  1410. * SMP-safe
  1411. */
  1412. int open_namei(int dfd, const char *pathname, int flag,
  1413. int mode, struct nameidata *nd)
  1414. {
  1415. int acc_mode, error;
  1416. struct path path;
  1417. struct dentry *dir;
  1418. int count = 0;
  1419. acc_mode = ACC_MODE(flag);
  1420. /* O_TRUNC implies we need access checks for write permissions */
  1421. if (flag & O_TRUNC)
  1422. acc_mode |= MAY_WRITE;
  1423. /* Allow the LSM permission hook to distinguish append
  1424. access from general write access. */
  1425. if (flag & O_APPEND)
  1426. acc_mode |= MAY_APPEND;
  1427. /*
  1428. * The simplest case - just a plain lookup.
  1429. */
  1430. if (!(flag & O_CREAT)) {
  1431. error = path_lookup_open(dfd, pathname, lookup_flags(flag),
  1432. nd, flag);
  1433. if (error)
  1434. return error;
  1435. goto ok;
  1436. }
  1437. /*
  1438. * Create - we need to know the parent.
  1439. */
  1440. error = path_lookup_create(dfd,pathname,LOOKUP_PARENT,nd,flag,mode);
  1441. if (error)
  1442. return error;
  1443. /*
  1444. * We have the parent and last component. First of all, check
  1445. * that we are not asked to creat(2) an obvious directory - that
  1446. * will not do.
  1447. */
  1448. error = -EISDIR;
  1449. if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
  1450. goto exit;
  1451. dir = nd->dentry;
  1452. nd->flags &= ~LOOKUP_PARENT;
  1453. mutex_lock(&dir->d_inode->i_mutex);
  1454. path.dentry = lookup_hash(nd);
  1455. path.mnt = nd->mnt;
  1456. do_last:
  1457. error = PTR_ERR(path.dentry);
  1458. if (IS_ERR(path.dentry)) {
  1459. mutex_unlock(&dir->d_inode->i_mutex);
  1460. goto exit;
  1461. }
  1462. if (IS_ERR(nd->intent.open.file)) {
  1463. mutex_unlock(&dir->d_inode->i_mutex);
  1464. error = PTR_ERR(nd->intent.open.file);
  1465. goto exit_dput;
  1466. }
  1467. /* Negative dentry, just create the file */
  1468. if (!path.dentry->d_inode) {
  1469. if (!IS_POSIXACL(dir->d_inode))
  1470. mode &= ~current->fs->umask;
  1471. error = vfs_create(dir->d_inode, path.dentry, mode, nd);
  1472. mutex_unlock(&dir->d_inode->i_mutex);
  1473. dput(nd->dentry);
  1474. nd->dentry = path.dentry;
  1475. if (error)
  1476. goto exit;
  1477. /* Don't check for write permission, don't truncate */
  1478. acc_mode = 0;
  1479. flag &= ~O_TRUNC;
  1480. goto ok;
  1481. }
  1482. /*
  1483. * It already exists.
  1484. */
  1485. mutex_unlock(&dir->d_inode->i_mutex);
  1486. error = -EEXIST;
  1487. if (flag & O_EXCL)
  1488. goto exit_dput;
  1489. if (__follow_mount(&path)) {
  1490. error = -ELOOP;
  1491. if (flag & O_NOFOLLOW)
  1492. goto exit_dput;
  1493. }
  1494. error = -ENOENT;
  1495. if (!path.dentry->d_inode)
  1496. goto exit_dput;
  1497. if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
  1498. goto do_link;
  1499. path_to_nameidata(&path, nd);
  1500. error = -EISDIR;
  1501. if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
  1502. goto exit;
  1503. ok:
  1504. error = may_open(nd, acc_mode, flag);
  1505. if (error)
  1506. goto exit;
  1507. return 0;
  1508. exit_dput:
  1509. dput_path(&path, nd);
  1510. exit:
  1511. if (!IS_ERR(nd->intent.open.file))
  1512. release_open_intent(nd);
  1513. path_release(nd);
  1514. return error;
  1515. do_link:
  1516. error = -ELOOP;
  1517. if (flag & O_NOFOLLOW)
  1518. goto exit_dput;
  1519. /*
  1520. * This is subtle. Instead of calling do_follow_link() we do the
  1521. * thing by hands. The reason is that this way we have zero link_count
  1522. * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
  1523. * After that we have the parent and last component, i.e.
  1524. * we are in the same situation as after the first path_walk().
  1525. * Well, almost - if the last component is normal we get its copy
  1526. * stored in nd->last.name and we will have to putname() it when we
  1527. * are done. Procfs-like symlinks just set LAST_BIND.
  1528. */
  1529. nd->flags |= LOOKUP_PARENT;
  1530. error = security_inode_follow_link(path.dentry, nd);
  1531. if (error)
  1532. goto exit_dput;
  1533. error = __do_follow_link(&path, nd);
  1534. if (error)
  1535. return error;
  1536. nd->flags &= ~LOOKUP_PARENT;
  1537. if (nd->last_type == LAST_BIND)
  1538. goto ok;
  1539. error = -EISDIR;
  1540. if (nd->last_type != LAST_NORM)
  1541. goto exit;
  1542. if (nd->last.name[nd->last.len]) {
  1543. __putname(nd->last.name);
  1544. goto exit;
  1545. }
  1546. error = -ELOOP;
  1547. if (count++==32) {
  1548. __putname(nd->last.name);
  1549. goto exit;
  1550. }
  1551. dir = nd->dentry;
  1552. mutex_lock(&dir->d_inode->i_mutex);
  1553. path.dentry = lookup_hash(nd);
  1554. path.mnt = nd->mnt;
  1555. __putname(nd->last.name);
  1556. goto do_last;
  1557. }
  1558. /**
  1559. * lookup_create - lookup a dentry, creating it if it doesn't exist
  1560. * @nd: nameidata info
  1561. * @is_dir: directory flag
  1562. *
  1563. * Simple function to lookup and return a dentry and create it
  1564. * if it doesn't exist. Is SMP-safe.
  1565. *
  1566. * Returns with nd->dentry->d_inode->i_mutex locked.
  1567. */
  1568. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  1569. {
  1570. struct dentry *dentry = ERR_PTR(-EEXIST);
  1571. mutex_lock(&nd->dentry->d_inode->i_mutex);
  1572. /*
  1573. * Yucky last component or no last component at all?
  1574. * (foo/., foo/.., /////)
  1575. */
  1576. if (nd->last_type != LAST_NORM)
  1577. goto fail;
  1578. nd->flags &= ~LOOKUP_PARENT;
  1579. /*
  1580. * Do the final lookup.
  1581. */
  1582. dentry = lookup_hash(nd);
  1583. if (IS_ERR(dentry))
  1584. goto fail;
  1585. /*
  1586. * Special case - lookup gave negative, but... we had foo/bar/
  1587. * From the vfs_mknod() POV we just have a negative dentry -
  1588. * all is fine. Let's be bastards - you had / on the end, you've
  1589. * been asking for (non-existent) directory. -ENOENT for you.
  1590. */
  1591. if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
  1592. goto enoent;
  1593. return dentry;
  1594. enoent:
  1595. dput(dentry);
  1596. dentry = ERR_PTR(-ENOENT);
  1597. fail:
  1598. return dentry;
  1599. }
  1600. EXPORT_SYMBOL_GPL(lookup_create);
  1601. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1602. {
  1603. int error = may_create(dir, dentry, NULL);
  1604. if (error)
  1605. return error;
  1606. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  1607. return -EPERM;
  1608. if (!dir->i_op || !dir->i_op->mknod)
  1609. return -EPERM;
  1610. error = security_inode_mknod(dir, dentry, mode, dev);
  1611. if (error)
  1612. return error;
  1613. DQUOT_INIT(dir);
  1614. error = dir->i_op->mknod(dir, dentry, mode, dev);
  1615. if (!error)
  1616. fsnotify_create(dir, dentry);
  1617. return error;
  1618. }
  1619. asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
  1620. unsigned dev)
  1621. {
  1622. int error = 0;
  1623. char * tmp;
  1624. struct dentry * dentry;
  1625. struct nameidata nd;
  1626. if (S_ISDIR(mode))
  1627. return -EPERM;
  1628. tmp = getname(filename);
  1629. if (IS_ERR(tmp))
  1630. return PTR_ERR(tmp);
  1631. error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
  1632. if (error)
  1633. goto out;
  1634. dentry = lookup_create(&nd, 0);
  1635. error = PTR_ERR(dentry);
  1636. if (!IS_POSIXACL(nd.dentry->d_inode))
  1637. mode &= ~current->fs->umask;
  1638. if (!IS_ERR(dentry)) {
  1639. switch (mode & S_IFMT) {
  1640. case 0: case S_IFREG:
  1641. error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
  1642. break;
  1643. case S_IFCHR: case S_IFBLK:
  1644. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
  1645. new_decode_dev(dev));
  1646. break;
  1647. case S_IFIFO: case S_IFSOCK:
  1648. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
  1649. break;
  1650. case S_IFDIR:
  1651. error = -EPERM;
  1652. break;
  1653. default:
  1654. error = -EINVAL;
  1655. }
  1656. dput(dentry);
  1657. }
  1658. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1659. path_release(&nd);
  1660. out:
  1661. putname(tmp);
  1662. return error;
  1663. }
  1664. asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
  1665. {
  1666. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  1667. }
  1668. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1669. {
  1670. int error = may_create(dir, dentry, NULL);
  1671. if (error)
  1672. return error;
  1673. if (!dir->i_op || !dir->i_op->mkdir)
  1674. return -EPERM;
  1675. mode &= (S_IRWXUGO|S_ISVTX);
  1676. error = security_inode_mkdir(dir, dentry, mode);
  1677. if (error)
  1678. return error;
  1679. DQUOT_INIT(dir);
  1680. error = dir->i_op->mkdir(dir, dentry, mode);
  1681. if (!error)
  1682. fsnotify_mkdir(dir, dentry);
  1683. return error;
  1684. }
  1685. asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
  1686. {
  1687. int error = 0;
  1688. char * tmp;
  1689. tmp = getname(pathname);
  1690. error = PTR_ERR(tmp);
  1691. if (!IS_ERR(tmp)) {
  1692. struct dentry *dentry;
  1693. struct nameidata nd;
  1694. error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
  1695. if (error)
  1696. goto out;
  1697. dentry = lookup_create(&nd, 1);
  1698. error = PTR_ERR(dentry);
  1699. if (!IS_ERR(dentry)) {
  1700. if (!IS_POSIXACL(nd.dentry->d_inode))
  1701. mode &= ~current->fs->umask;
  1702. error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
  1703. dput(dentry);
  1704. }
  1705. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1706. path_release(&nd);
  1707. out:
  1708. putname(tmp);
  1709. }
  1710. return error;
  1711. }
  1712. asmlinkage long sys_mkdir(const char __user *pathname, int mode)
  1713. {
  1714. return sys_mkdirat(AT_FDCWD, pathname, mode);
  1715. }
  1716. /*
  1717. * We try to drop the dentry early: we should have
  1718. * a usage count of 2 if we're the only user of this
  1719. * dentry, and if that is true (possibly after pruning
  1720. * the dcache), then we drop the dentry now.
  1721. *
  1722. * A low-level filesystem can, if it choses, legally
  1723. * do a
  1724. *
  1725. * if (!d_unhashed(dentry))
  1726. * return -EBUSY;
  1727. *
  1728. * if it cannot handle the case of removing a directory
  1729. * that is still in use by something else..
  1730. */
  1731. void dentry_unhash(struct dentry *dentry)
  1732. {
  1733. dget(dentry);
  1734. if (atomic_read(&dentry->d_count))
  1735. shrink_dcache_parent(dentry);
  1736. spin_lock(&dcache_lock);
  1737. spin_lock(&dentry->d_lock);
  1738. if (atomic_read(&dentry->d_count) == 2)
  1739. __d_drop(dentry);
  1740. spin_unlock(&dentry->d_lock);
  1741. spin_unlock(&dcache_lock);
  1742. }
  1743. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  1744. {
  1745. int error = may_delete(dir, dentry, 1);
  1746. if (error)
  1747. return error;
  1748. if (!dir->i_op || !dir->i_op->rmdir)
  1749. return -EPERM;
  1750. DQUOT_INIT(dir);
  1751. mutex_lock(&dentry->d_inode->i_mutex);
  1752. dentry_unhash(dentry);
  1753. if (d_mountpoint(dentry))
  1754. error = -EBUSY;
  1755. else {
  1756. error = security_inode_rmdir(dir, dentry);
  1757. if (!error) {
  1758. error = dir->i_op->rmdir(dir, dentry);
  1759. if (!error)
  1760. dentry->d_inode->i_flags |= S_DEAD;
  1761. }
  1762. }
  1763. mutex_unlock(&dentry->d_inode->i_mutex);
  1764. if (!error) {
  1765. d_delete(dentry);
  1766. }
  1767. dput(dentry);
  1768. return error;
  1769. }
  1770. static long do_rmdir(int dfd, const char __user *pathname)
  1771. {
  1772. int error = 0;
  1773. char * name;
  1774. struct dentry *dentry;
  1775. struct nameidata nd;
  1776. name = getname(pathname);
  1777. if(IS_ERR(name))
  1778. return PTR_ERR(name);
  1779. error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
  1780. if (error)
  1781. goto exit;
  1782. switch(nd.last_type) {
  1783. case LAST_DOTDOT:
  1784. error = -ENOTEMPTY;
  1785. goto exit1;
  1786. case LAST_DOT:
  1787. error = -EINVAL;
  1788. goto exit1;
  1789. case LAST_ROOT:
  1790. error = -EBUSY;
  1791. goto exit1;
  1792. }
  1793. mutex_lock(&nd.dentry->d_inode->i_mutex);
  1794. dentry = lookup_hash(&nd);
  1795. error = PTR_ERR(dentry);
  1796. if (!IS_ERR(dentry)) {
  1797. error = vfs_rmdir(nd.dentry->d_inode, dentry);
  1798. dput(dentry);
  1799. }
  1800. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1801. exit1:
  1802. path_release(&nd);
  1803. exit:
  1804. putname(name);
  1805. return error;
  1806. }
  1807. asmlinkage long sys_rmdir(const char __user *pathname)
  1808. {
  1809. return do_rmdir(AT_FDCWD, pathname);
  1810. }
  1811. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  1812. {
  1813. int error = may_delete(dir, dentry, 0);
  1814. if (error)
  1815. return error;
  1816. if (!dir->i_op || !dir->i_op->unlink)
  1817. return -EPERM;
  1818. DQUOT_INIT(dir);
  1819. mutex_lock(&dentry->d_inode->i_mutex);
  1820. if (d_mountpoint(dentry))
  1821. error = -EBUSY;
  1822. else {
  1823. error = security_inode_unlink(dir, dentry);
  1824. if (!error)
  1825. error = dir->i_op->unlink(dir, dentry);
  1826. }
  1827. mutex_unlock(&dentry->d_inode->i_mutex);
  1828. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  1829. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  1830. d_delete(dentry);
  1831. }
  1832. return error;
  1833. }
  1834. /*
  1835. * Make sure that the actual truncation of the file will occur outside its
  1836. * directory's i_mutex. Truncate can take a long time if there is a lot of
  1837. * writeout happening, and we don't want to prevent access to the directory
  1838. * while waiting on the I/O.
  1839. */
  1840. static long do_unlinkat(int dfd, const char __user *pathname)
  1841. {
  1842. int error = 0;
  1843. char * name;
  1844. struct dentry *dentry;
  1845. struct nameidata nd;
  1846. struct inode *inode = NULL;
  1847. name = getname(pathname);
  1848. if(IS_ERR(name))
  1849. return PTR_ERR(name);
  1850. error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
  1851. if (error)
  1852. goto exit;
  1853. error = -EISDIR;
  1854. if (nd.last_type != LAST_NORM)
  1855. goto exit1;
  1856. mutex_lock(&nd.dentry->d_inode->i_mutex);
  1857. dentry = lookup_hash(&nd);
  1858. error = PTR_ERR(dentry);
  1859. if (!IS_ERR(dentry)) {
  1860. /* Why not before? Because we want correct error value */
  1861. if (nd.last.name[nd.last.len])
  1862. goto slashes;
  1863. inode = dentry->d_inode;
  1864. if (inode)
  1865. atomic_inc(&inode->i_count);
  1866. error = vfs_unlink(nd.dentry->d_inode, dentry);
  1867. exit2:
  1868. dput(dentry);
  1869. }
  1870. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1871. if (inode)
  1872. iput(inode); /* truncate the inode here */
  1873. exit1:
  1874. path_release(&nd);
  1875. exit:
  1876. putname(name);
  1877. return error;
  1878. slashes:
  1879. error = !dentry->d_inode ? -ENOENT :
  1880. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  1881. goto exit2;
  1882. }
  1883. asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
  1884. {
  1885. if ((flag & ~AT_REMOVEDIR) != 0)
  1886. return -EINVAL;
  1887. if (flag & AT_REMOVEDIR)
  1888. return do_rmdir(dfd, pathname);
  1889. return do_unlinkat(dfd, pathname);
  1890. }
  1891. asmlinkage long sys_unlink(const char __user *pathname)
  1892. {
  1893. return do_unlinkat(AT_FDCWD, pathname);
  1894. }
  1895. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
  1896. {
  1897. int error = may_create(dir, dentry, NULL);
  1898. if (error)
  1899. return error;
  1900. if (!dir->i_op || !dir->i_op->symlink)
  1901. return -EPERM;
  1902. error = security_inode_symlink(dir, dentry, oldname);
  1903. if (error)
  1904. return error;
  1905. DQUOT_INIT(dir);
  1906. error = dir->i_op->symlink(dir, dentry, oldname);
  1907. if (!error)
  1908. fsnotify_create(dir, dentry);
  1909. return error;
  1910. }
  1911. asmlinkage long sys_symlinkat(const char __user *oldname,
  1912. int newdfd, const char __user *newname)
  1913. {
  1914. int error = 0;
  1915. char * from;
  1916. char * to;
  1917. from = getname(oldname);
  1918. if(IS_ERR(from))
  1919. return PTR_ERR(from);
  1920. to = getname(newname);
  1921. error = PTR_ERR(to);
  1922. if (!IS_ERR(to)) {
  1923. struct dentry *dentry;
  1924. struct nameidata nd;
  1925. error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
  1926. if (error)
  1927. goto out;
  1928. dentry = lookup_create(&nd, 0);
  1929. error = PTR_ERR(dentry);
  1930. if (!IS_ERR(dentry)) {
  1931. error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
  1932. dput(dentry);
  1933. }
  1934. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1935. path_release(&nd);
  1936. out:
  1937. putname(to);
  1938. }
  1939. putname(from);
  1940. return error;
  1941. }
  1942. asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
  1943. {
  1944. return sys_symlinkat(oldname, AT_FDCWD, newname);
  1945. }
  1946. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  1947. {
  1948. struct inode *inode = old_dentry->d_inode;
  1949. int error;
  1950. if (!inode)
  1951. return -ENOENT;
  1952. error = may_create(dir, new_dentry, NULL);
  1953. if (error)
  1954. return error;
  1955. if (dir->i_sb != inode->i_sb)
  1956. return -EXDEV;
  1957. /*
  1958. * A link to an append-only or immutable file cannot be created.
  1959. */
  1960. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1961. return -EPERM;
  1962. if (!dir->i_op || !dir->i_op->link)
  1963. return -EPERM;
  1964. if (S_ISDIR(old_dentry->d_inode->i_mode))
  1965. return -EPERM;
  1966. error = security_inode_link(old_dentry, dir, new_dentry);
  1967. if (error)
  1968. return error;
  1969. mutex_lock(&old_dentry->d_inode->i_mutex);
  1970. DQUOT_INIT(dir);
  1971. error = dir->i_op->link(old_dentry, dir, new_dentry);
  1972. mutex_unlock(&old_dentry->d_inode->i_mutex);
  1973. if (!error)
  1974. fsnotify_create(dir, new_dentry);
  1975. return error;
  1976. }
  1977. /*
  1978. * Hardlinks are often used in delicate situations. We avoid
  1979. * security-related surprises by not following symlinks on the
  1980. * newname. --KAB
  1981. *
  1982. * We don't follow them on the oldname either to be compatible
  1983. * with linux 2.0, and to avoid hard-linking to directories
  1984. * and other special files. --ADM
  1985. */
  1986. asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
  1987. int newdfd, const char __user *newname,
  1988. int flags)
  1989. {
  1990. struct dentry *new_dentry;
  1991. struct nameidata nd, old_nd;
  1992. int error;
  1993. char * to;
  1994. if (flags != 0)
  1995. return -EINVAL;
  1996. to = getname(newname);
  1997. if (IS_ERR(to))
  1998. return PTR_ERR(to);
  1999. error = __user_walk_fd(olddfd, oldname, 0, &old_nd);
  2000. if (error)
  2001. goto exit;
  2002. error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
  2003. if (error)
  2004. goto out;
  2005. error = -EXDEV;
  2006. if (old_nd.mnt != nd.mnt)
  2007. goto out_release;
  2008. new_dentry = lookup_create(&nd, 0);
  2009. error = PTR_ERR(new_dentry);
  2010. if (!IS_ERR(new_dentry)) {
  2011. error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
  2012. dput(new_dentry);
  2013. }
  2014. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  2015. out_release:
  2016. path_release(&nd);
  2017. out:
  2018. path_release(&old_nd);
  2019. exit:
  2020. putname(to);
  2021. return error;
  2022. }
  2023. asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
  2024. {
  2025. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2026. }
  2027. /*
  2028. * The worst of all namespace operations - renaming directory. "Perverted"
  2029. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2030. * Problems:
  2031. * a) we can get into loop creation. Check is done in is_subdir().
  2032. * b) race potential - two innocent renames can create a loop together.
  2033. * That's where 4.4 screws up. Current fix: serialization on
  2034. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2035. * story.
  2036. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2037. * And that - after we got ->i_mutex on parents (until then we don't know
  2038. * whether the target exists). Solution: try to be smart with locking
  2039. * order for inodes. We rely on the fact that tree topology may change
  2040. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2041. * move will be locked. Thus we can rank directories by the tree
  2042. * (ancestors first) and rank all non-directories after them.
  2043. * That works since everybody except rename does "lock parent, lookup,
  2044. * lock child" and rename is under ->s_vfs_rename_mutex.
  2045. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2046. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2047. * we'd better make sure that there's no link(2) for them.
  2048. * d) some filesystems don't support opened-but-unlinked directories,
  2049. * either because of layout or because they are not ready to deal with
  2050. * all cases correctly. The latter will be fixed (taking this sort of
  2051. * stuff into VFS), but the former is not going away. Solution: the same
  2052. * trick as in rmdir().
  2053. * e) conversion from fhandle to dentry may come in the wrong moment - when
  2054. * we are removing the target. Solution: we will have to grab ->i_mutex
  2055. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2056. * ->i_mutex on parents, which works but leads to some truely excessive
  2057. * locking].
  2058. */
  2059. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2060. struct inode *new_dir, struct dentry *new_dentry)
  2061. {
  2062. int error = 0;
  2063. struct inode *target;
  2064. /*
  2065. * If we are going to change the parent - check write permissions,
  2066. * we'll need to flip '..'.
  2067. */
  2068. if (new_dir != old_dir) {
  2069. error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
  2070. if (error)
  2071. return error;
  2072. }
  2073. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2074. if (error)
  2075. return error;
  2076. target = new_dentry->d_inode;
  2077. if (target) {
  2078. mutex_lock(&target->i_mutex);
  2079. dentry_unhash(new_dentry);
  2080. }
  2081. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2082. error = -EBUSY;
  2083. else
  2084. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2085. if (target) {
  2086. if (!error)
  2087. target->i_flags |= S_DEAD;
  2088. mutex_unlock(&target->i_mutex);
  2089. if (d_unhashed(new_dentry))
  2090. d_rehash(new_dentry);
  2091. dput(new_dentry);
  2092. }
  2093. if (!error)
  2094. d_move(old_dentry,new_dentry);
  2095. return error;
  2096. }
  2097. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2098. struct inode *new_dir, struct dentry *new_dentry)
  2099. {
  2100. struct inode *target;
  2101. int error;
  2102. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2103. if (error)
  2104. return error;
  2105. dget(new_dentry);
  2106. target = new_dentry->d_inode;
  2107. if (target)
  2108. mutex_lock(&target->i_mutex);
  2109. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2110. error = -EBUSY;
  2111. else
  2112. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2113. if (!error) {
  2114. /* The following d_move() should become unconditional */
  2115. if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
  2116. d_move(old_dentry, new_dentry);
  2117. }
  2118. if (target)
  2119. mutex_unlock(&target->i_mutex);
  2120. dput(new_dentry);
  2121. return error;
  2122. }
  2123. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2124. struct inode *new_dir, struct dentry *new_dentry)
  2125. {
  2126. int error;
  2127. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2128. const char *old_name;
  2129. if (old_dentry->d_inode == new_dentry->d_inode)
  2130. return 0;
  2131. error = may_delete(old_dir, old_dentry, is_dir);
  2132. if (error)
  2133. return error;
  2134. if (!new_dentry->d_inode)
  2135. error = may_create(new_dir, new_dentry, NULL);
  2136. else
  2137. error = may_delete(new_dir, new_dentry, is_dir);
  2138. if (error)
  2139. return error;
  2140. if (!old_dir->i_op || !old_dir->i_op->rename)
  2141. return -EPERM;
  2142. DQUOT_INIT(old_dir);
  2143. DQUOT_INIT(new_dir);
  2144. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2145. if (is_dir)
  2146. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2147. else
  2148. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2149. if (!error) {
  2150. const char *new_name = old_dentry->d_name.name;
  2151. fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
  2152. new_dentry->d_inode, old_dentry->d_inode);
  2153. }
  2154. fsnotify_oldname_free(old_name);
  2155. return error;
  2156. }
  2157. static int do_rename(int olddfd, const char *oldname,
  2158. int newdfd, const char *newname)
  2159. {
  2160. int error = 0;
  2161. struct dentry * old_dir, * new_dir;
  2162. struct dentry * old_dentry, *new_dentry;
  2163. struct dentry * trap;
  2164. struct nameidata oldnd, newnd;
  2165. error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
  2166. if (error)
  2167. goto exit;
  2168. error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
  2169. if (error)
  2170. goto exit1;
  2171. error = -EXDEV;
  2172. if (oldnd.mnt != newnd.mnt)
  2173. goto exit2;
  2174. old_dir = oldnd.dentry;
  2175. error = -EBUSY;
  2176. if (oldnd.last_type != LAST_NORM)
  2177. goto exit2;
  2178. new_dir = newnd.dentry;
  2179. if (newnd.last_type != LAST_NORM)
  2180. goto exit2;
  2181. trap = lock_rename(new_dir, old_dir);
  2182. old_dentry = lookup_hash(&oldnd);
  2183. error = PTR_ERR(old_dentry);
  2184. if (IS_ERR(old_dentry))
  2185. goto exit3;
  2186. /* source must exist */
  2187. error = -ENOENT;
  2188. if (!old_dentry->d_inode)
  2189. goto exit4;
  2190. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2191. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2192. error = -ENOTDIR;
  2193. if (oldnd.last.name[oldnd.last.len])
  2194. goto exit4;
  2195. if (newnd.last.name[newnd.last.len])
  2196. goto exit4;
  2197. }
  2198. /* source should not be ancestor of target */
  2199. error = -EINVAL;
  2200. if (old_dentry == trap)
  2201. goto exit4;
  2202. new_dentry = lookup_hash(&newnd);
  2203. error = PTR_ERR(new_dentry);
  2204. if (IS_ERR(new_dentry))
  2205. goto exit4;
  2206. /* target should not be an ancestor of source */
  2207. error = -ENOTEMPTY;
  2208. if (new_dentry == trap)
  2209. goto exit5;
  2210. error = vfs_rename(old_dir->d_inode, old_dentry,
  2211. new_dir->d_inode, new_dentry);
  2212. exit5:
  2213. dput(new_dentry);
  2214. exit4:
  2215. dput(old_dentry);
  2216. exit3:
  2217. unlock_rename(new_dir, old_dir);
  2218. exit2:
  2219. path_release(&newnd);
  2220. exit1:
  2221. path_release(&oldnd);
  2222. exit:
  2223. return error;
  2224. }
  2225. asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
  2226. int newdfd, const char __user *newname)
  2227. {
  2228. int error;
  2229. char * from;
  2230. char * to;
  2231. from = getname(oldname);
  2232. if(IS_ERR(from))
  2233. return PTR_ERR(from);
  2234. to = getname(newname);
  2235. error = PTR_ERR(to);
  2236. if (!IS_ERR(to)) {
  2237. error = do_rename(olddfd, from, newdfd, to);
  2238. putname(to);
  2239. }
  2240. putname(from);
  2241. return error;
  2242. }
  2243. asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
  2244. {
  2245. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2246. }
  2247. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2248. {
  2249. int len;
  2250. len = PTR_ERR(link);
  2251. if (IS_ERR(link))
  2252. goto out;
  2253. len = strlen(link);
  2254. if (len > (unsigned) buflen)
  2255. len = buflen;
  2256. if (copy_to_user(buffer, link, len))
  2257. len = -EFAULT;
  2258. out:
  2259. return len;
  2260. }
  2261. /*
  2262. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2263. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2264. * using) it for any given inode is up to filesystem.
  2265. */
  2266. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2267. {
  2268. struct nameidata nd;
  2269. void *cookie;
  2270. nd.depth = 0;
  2271. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2272. if (!IS_ERR(cookie)) {
  2273. int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2274. if (dentry->d_inode->i_op->put_link)
  2275. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2276. cookie = ERR_PTR(res);
  2277. }
  2278. return PTR_ERR(cookie);
  2279. }
  2280. int vfs_follow_link(struct nameidata *nd, const char *link)
  2281. {
  2282. return __vfs_follow_link(nd, link);
  2283. }
  2284. /* get the link contents into pagecache */
  2285. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2286. {
  2287. struct page * page;
  2288. struct address_space *mapping = dentry->d_inode->i_mapping;
  2289. page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
  2290. NULL);
  2291. if (IS_ERR(page))
  2292. goto sync_fail;
  2293. wait_on_page_locked(page);
  2294. if (!PageUptodate(page))
  2295. goto async_fail;
  2296. *ppage = page;
  2297. return kmap(page);
  2298. async_fail:
  2299. page_cache_release(page);
  2300. return ERR_PTR(-EIO);
  2301. sync_fail:
  2302. return (char*)page;
  2303. }
  2304. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2305. {
  2306. struct page *page = NULL;
  2307. char *s = page_getlink(dentry, &page);
  2308. int res = vfs_readlink(dentry,buffer,buflen,s);
  2309. if (page) {
  2310. kunmap(page);
  2311. page_cache_release(page);
  2312. }
  2313. return res;
  2314. }
  2315. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2316. {
  2317. struct page *page = NULL;
  2318. nd_set_link(nd, page_getlink(dentry, &page));
  2319. return page;
  2320. }
  2321. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2322. {
  2323. struct page *page = cookie;
  2324. if (page) {
  2325. kunmap(page);
  2326. page_cache_release(page);
  2327. }
  2328. }
  2329. int __page_symlink(struct inode *inode, const char *symname, int len,
  2330. gfp_t gfp_mask)
  2331. {
  2332. struct address_space *mapping = inode->i_mapping;
  2333. struct page *page;
  2334. int err = -ENOMEM;
  2335. char *kaddr;
  2336. retry:
  2337. page = find_or_create_page(mapping, 0, gfp_mask);
  2338. if (!page)
  2339. goto fail;
  2340. err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
  2341. if (err == AOP_TRUNCATED_PAGE) {
  2342. page_cache_release(page);
  2343. goto retry;
  2344. }
  2345. if (err)
  2346. goto fail_map;
  2347. kaddr = kmap_atomic(page, KM_USER0);
  2348. memcpy(kaddr, symname, len-1);
  2349. kunmap_atomic(kaddr, KM_USER0);
  2350. err = mapping->a_ops->commit_write(NULL, page, 0, len-1);
  2351. if (err == AOP_TRUNCATED_PAGE) {
  2352. page_cache_release(page);
  2353. goto retry;
  2354. }
  2355. if (err)
  2356. goto fail_map;
  2357. /*
  2358. * Notice that we are _not_ going to block here - end of page is
  2359. * unmapped, so this will only try to map the rest of page, see
  2360. * that it is unmapped (typically even will not look into inode -
  2361. * ->i_size will be enough for everything) and zero it out.
  2362. * OTOH it's obviously correct and should make the page up-to-date.
  2363. */
  2364. if (!PageUptodate(page)) {
  2365. err = mapping->a_ops->readpage(NULL, page);
  2366. if (err != AOP_TRUNCATED_PAGE)
  2367. wait_on_page_locked(page);
  2368. } else {
  2369. unlock_page(page);
  2370. }
  2371. page_cache_release(page);
  2372. if (err < 0)
  2373. goto fail;
  2374. mark_inode_dirty(inode);
  2375. return 0;
  2376. fail_map:
  2377. unlock_page(page);
  2378. page_cache_release(page);
  2379. fail:
  2380. return err;
  2381. }
  2382. int page_symlink(struct inode *inode, const char *symname, int len)
  2383. {
  2384. return __page_symlink(inode, symname, len,
  2385. mapping_gfp_mask(inode->i_mapping));
  2386. }
  2387. struct inode_operations page_symlink_inode_operations = {
  2388. .readlink = generic_readlink,
  2389. .follow_link = page_follow_link_light,
  2390. .put_link = page_put_link,
  2391. };
  2392. EXPORT_SYMBOL(__user_walk);
  2393. EXPORT_SYMBOL(__user_walk_fd);
  2394. EXPORT_SYMBOL(follow_down);
  2395. EXPORT_SYMBOL(follow_up);
  2396. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2397. EXPORT_SYMBOL(getname);
  2398. EXPORT_SYMBOL(lock_rename);
  2399. EXPORT_SYMBOL(lookup_one_len);
  2400. EXPORT_SYMBOL(page_follow_link_light);
  2401. EXPORT_SYMBOL(page_put_link);
  2402. EXPORT_SYMBOL(page_readlink);
  2403. EXPORT_SYMBOL(__page_symlink);
  2404. EXPORT_SYMBOL(page_symlink);
  2405. EXPORT_SYMBOL(page_symlink_inode_operations);
  2406. EXPORT_SYMBOL(path_lookup);
  2407. EXPORT_SYMBOL(path_release);
  2408. EXPORT_SYMBOL(path_walk);
  2409. EXPORT_SYMBOL(permission);
  2410. EXPORT_SYMBOL(vfs_permission);
  2411. EXPORT_SYMBOL(file_permission);
  2412. EXPORT_SYMBOL(unlock_rename);
  2413. EXPORT_SYMBOL(vfs_create);
  2414. EXPORT_SYMBOL(vfs_follow_link);
  2415. EXPORT_SYMBOL(vfs_link);
  2416. EXPORT_SYMBOL(vfs_mkdir);
  2417. EXPORT_SYMBOL(vfs_mknod);
  2418. EXPORT_SYMBOL(generic_permission);
  2419. EXPORT_SYMBOL(vfs_readlink);
  2420. EXPORT_SYMBOL(vfs_rename);
  2421. EXPORT_SYMBOL(vfs_rmdir);
  2422. EXPORT_SYMBOL(vfs_symlink);
  2423. EXPORT_SYMBOL(vfs_unlink);
  2424. EXPORT_SYMBOL(dentry_unhash);
  2425. EXPORT_SYMBOL(generic_readlink);