namei.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287
  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/pagemap.h>
  20. #include <linux/fsnotify.h>
  21. #include <linux/personality.h>
  22. #include <linux/security.h>
  23. #include <linux/ima.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/mount.h>
  26. #include <linux/audit.h>
  27. #include <linux/capability.h>
  28. #include <linux/file.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/device_cgroup.h>
  31. #include <linux/fs_struct.h>
  32. #include <asm/uaccess.h>
  33. #include "internal.h"
  34. /* [Feb-1997 T. Schoebel-Theuer]
  35. * Fundamental changes in the pathname lookup mechanisms (namei)
  36. * were necessary because of omirr. The reason is that omirr needs
  37. * to know the _real_ pathname, not the user-supplied one, in case
  38. * of symlinks (and also when transname replacements occur).
  39. *
  40. * The new code replaces the old recursive symlink resolution with
  41. * an iterative one (in case of non-nested symlink chains). It does
  42. * this with calls to <fs>_follow_link().
  43. * As a side effect, dir_namei(), _namei() and follow_link() are now
  44. * replaced with a single function lookup_dentry() that can handle all
  45. * the special cases of the former code.
  46. *
  47. * With the new dcache, the pathname is stored at each inode, at least as
  48. * long as the refcount of the inode is positive. As a side effect, the
  49. * size of the dcache depends on the inode cache and thus is dynamic.
  50. *
  51. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  52. * resolution to correspond with current state of the code.
  53. *
  54. * Note that the symlink resolution is not *completely* iterative.
  55. * There is still a significant amount of tail- and mid- recursion in
  56. * the algorithm. Also, note that <fs>_readlink() is not used in
  57. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  58. * may return different results than <fs>_follow_link(). Many virtual
  59. * filesystems (including /proc) exhibit this behavior.
  60. */
  61. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  62. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  63. * and the name already exists in form of a symlink, try to create the new
  64. * name indicated by the symlink. The old code always complained that the
  65. * name already exists, due to not following the symlink even if its target
  66. * is nonexistent. The new semantics affects also mknod() and link() when
  67. * the name is a symlink pointing to a non-existent name.
  68. *
  69. * I don't know which semantics is the right one, since I have no access
  70. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  71. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  72. * "old" one. Personally, I think the new semantics is much more logical.
  73. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  74. * file does succeed in both HP-UX and SunOs, but not in Solaris
  75. * and in the old Linux semantics.
  76. */
  77. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  78. * semantics. See the comments in "open_namei" and "do_link" below.
  79. *
  80. * [10-Sep-98 Alan Modra] Another symlink change.
  81. */
  82. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  83. * inside the path - always follow.
  84. * in the last component in creation/removal/renaming - never follow.
  85. * if LOOKUP_FOLLOW passed - follow.
  86. * if the pathname has trailing slashes - follow.
  87. * otherwise - don't follow.
  88. * (applied in that order).
  89. *
  90. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  91. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  92. * During the 2.4 we need to fix the userland stuff depending on it -
  93. * hopefully we will be able to get rid of that wart in 2.5. So far only
  94. * XEmacs seems to be relying on it...
  95. */
  96. /*
  97. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  98. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  99. * any extra contention...
  100. */
  101. /* In order to reduce some races, while at the same time doing additional
  102. * checking and hopefully speeding things up, we copy filenames to the
  103. * kernel data space before using them..
  104. *
  105. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  106. * PATH_MAX includes the nul terminator --RR.
  107. */
  108. static int do_getname(const char __user *filename, char *page)
  109. {
  110. int retval;
  111. unsigned long len = PATH_MAX;
  112. if (!segment_eq(get_fs(), KERNEL_DS)) {
  113. if ((unsigned long) filename >= TASK_SIZE)
  114. return -EFAULT;
  115. if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
  116. len = TASK_SIZE - (unsigned long) filename;
  117. }
  118. retval = strncpy_from_user(page, filename, len);
  119. if (retval > 0) {
  120. if (retval < len)
  121. return 0;
  122. return -ENAMETOOLONG;
  123. } else if (!retval)
  124. retval = -ENOENT;
  125. return retval;
  126. }
  127. static char *getname_flags(const char __user * filename, int flags)
  128. {
  129. char *tmp, *result;
  130. result = ERR_PTR(-ENOMEM);
  131. tmp = __getname();
  132. if (tmp) {
  133. int retval = do_getname(filename, tmp);
  134. result = tmp;
  135. if (retval < 0) {
  136. if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
  137. __putname(tmp);
  138. result = ERR_PTR(retval);
  139. }
  140. }
  141. }
  142. audit_getname(result);
  143. return result;
  144. }
  145. char *getname(const char __user * filename)
  146. {
  147. return getname_flags(filename, 0);
  148. }
  149. #ifdef CONFIG_AUDITSYSCALL
  150. void putname(const char *name)
  151. {
  152. if (unlikely(!audit_dummy_context()))
  153. audit_putname(name);
  154. else
  155. __putname(name);
  156. }
  157. EXPORT_SYMBOL(putname);
  158. #endif
  159. /*
  160. * This does basic POSIX ACL permission checking
  161. */
  162. static int acl_permission_check(struct inode *inode, int mask)
  163. {
  164. int (*check_acl)(struct inode *inode, int mask);
  165. unsigned int mode = inode->i_mode;
  166. mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
  167. if (current_user_ns() != inode_userns(inode))
  168. goto other_perms;
  169. if (current_fsuid() == inode->i_uid)
  170. mode >>= 6;
  171. else {
  172. check_acl = inode->i_op->check_acl;
  173. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  174. int error = check_acl(inode, mask);
  175. if (error != -EAGAIN)
  176. return error;
  177. }
  178. if (in_group_p(inode->i_gid))
  179. mode >>= 3;
  180. }
  181. other_perms:
  182. /*
  183. * If the DACs are ok we don't need any capability check.
  184. */
  185. if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  186. return 0;
  187. return -EACCES;
  188. }
  189. /**
  190. * generic_permission - check for access rights on a Posix-like filesystem
  191. * @inode: inode to check access rights for
  192. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  193. *
  194. * Used to check for read/write/execute permissions on a file.
  195. * We use "fsuid" for this, letting us set arbitrary permissions
  196. * for filesystem access without changing the "normal" uids which
  197. * are used for other things.
  198. *
  199. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  200. * request cannot be satisfied (eg. requires blocking or too much complexity).
  201. * It would then be called again in ref-walk mode.
  202. */
  203. int generic_permission(struct inode *inode, int mask)
  204. {
  205. int ret;
  206. /*
  207. * Do the basic POSIX ACL permission checks.
  208. */
  209. ret = acl_permission_check(inode, mask);
  210. if (ret != -EACCES)
  211. return ret;
  212. if (S_ISDIR(inode->i_mode)) {
  213. /* DACs are overridable for directories */
  214. if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
  215. return 0;
  216. if (!(mask & MAY_WRITE))
  217. if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
  218. return 0;
  219. return -EACCES;
  220. }
  221. /*
  222. * Read/write DACs are always overridable.
  223. * Executable DACs are overridable when there is
  224. * at least one exec bit set.
  225. */
  226. if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
  227. if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
  228. return 0;
  229. /*
  230. * Searching includes executable on directories, else just read.
  231. */
  232. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  233. if (mask == MAY_READ)
  234. if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
  235. return 0;
  236. return -EACCES;
  237. }
  238. /**
  239. * inode_permission - check for access rights to a given inode
  240. * @inode: inode to check permission on
  241. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  242. *
  243. * Used to check for read/write/execute permissions on an inode.
  244. * We use "fsuid" for this, letting us set arbitrary permissions
  245. * for filesystem access without changing the "normal" uids which
  246. * are used for other things.
  247. */
  248. int inode_permission(struct inode *inode, int mask)
  249. {
  250. int retval;
  251. if (mask & MAY_WRITE) {
  252. umode_t mode = inode->i_mode;
  253. /*
  254. * Nobody gets write access to a read-only fs.
  255. */
  256. if (IS_RDONLY(inode) &&
  257. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  258. return -EROFS;
  259. /*
  260. * Nobody gets write access to an immutable file.
  261. */
  262. if (IS_IMMUTABLE(inode))
  263. return -EACCES;
  264. }
  265. if (inode->i_op->permission)
  266. retval = inode->i_op->permission(inode, mask);
  267. else
  268. retval = generic_permission(inode, mask);
  269. if (retval)
  270. return retval;
  271. retval = devcgroup_inode_permission(inode, mask);
  272. if (retval)
  273. return retval;
  274. return security_inode_permission(inode, mask);
  275. }
  276. /**
  277. * path_get - get a reference to a path
  278. * @path: path to get the reference to
  279. *
  280. * Given a path increment the reference count to the dentry and the vfsmount.
  281. */
  282. void path_get(struct path *path)
  283. {
  284. mntget(path->mnt);
  285. dget(path->dentry);
  286. }
  287. EXPORT_SYMBOL(path_get);
  288. /**
  289. * path_put - put a reference to a path
  290. * @path: path to put the reference to
  291. *
  292. * Given a path decrement the reference count to the dentry and the vfsmount.
  293. */
  294. void path_put(struct path *path)
  295. {
  296. dput(path->dentry);
  297. mntput(path->mnt);
  298. }
  299. EXPORT_SYMBOL(path_put);
  300. /*
  301. * Path walking has 2 modes, rcu-walk and ref-walk (see
  302. * Documentation/filesystems/path-lookup.txt). In situations when we can't
  303. * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
  304. * normal reference counts on dentries and vfsmounts to transition to rcu-walk
  305. * mode. Refcounts are grabbed at the last known good point before rcu-walk
  306. * got stuck, so ref-walk may continue from there. If this is not successful
  307. * (eg. a seqcount has changed), then failure is returned and it's up to caller
  308. * to restart the path walk from the beginning in ref-walk mode.
  309. */
  310. /**
  311. * unlazy_walk - try to switch to ref-walk mode.
  312. * @nd: nameidata pathwalk data
  313. * @dentry: child of nd->path.dentry or NULL
  314. * Returns: 0 on success, -ECHILD on failure
  315. *
  316. * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
  317. * for ref-walk mode. @dentry must be a path found by a do_lookup call on
  318. * @nd or NULL. Must be called from rcu-walk context.
  319. */
  320. static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
  321. {
  322. struct fs_struct *fs = current->fs;
  323. struct dentry *parent = nd->path.dentry;
  324. int want_root = 0;
  325. BUG_ON(!(nd->flags & LOOKUP_RCU));
  326. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  327. want_root = 1;
  328. spin_lock(&fs->lock);
  329. if (nd->root.mnt != fs->root.mnt ||
  330. nd->root.dentry != fs->root.dentry)
  331. goto err_root;
  332. }
  333. spin_lock(&parent->d_lock);
  334. if (!dentry) {
  335. if (!__d_rcu_to_refcount(parent, nd->seq))
  336. goto err_parent;
  337. BUG_ON(nd->inode != parent->d_inode);
  338. } else {
  339. if (dentry->d_parent != parent)
  340. goto err_parent;
  341. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  342. if (!__d_rcu_to_refcount(dentry, nd->seq))
  343. goto err_child;
  344. /*
  345. * If the sequence check on the child dentry passed, then
  346. * the child has not been removed from its parent. This
  347. * means the parent dentry must be valid and able to take
  348. * a reference at this point.
  349. */
  350. BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
  351. BUG_ON(!parent->d_count);
  352. parent->d_count++;
  353. spin_unlock(&dentry->d_lock);
  354. }
  355. spin_unlock(&parent->d_lock);
  356. if (want_root) {
  357. path_get(&nd->root);
  358. spin_unlock(&fs->lock);
  359. }
  360. mntget(nd->path.mnt);
  361. rcu_read_unlock();
  362. br_read_unlock(vfsmount_lock);
  363. nd->flags &= ~LOOKUP_RCU;
  364. return 0;
  365. err_child:
  366. spin_unlock(&dentry->d_lock);
  367. err_parent:
  368. spin_unlock(&parent->d_lock);
  369. err_root:
  370. if (want_root)
  371. spin_unlock(&fs->lock);
  372. return -ECHILD;
  373. }
  374. /**
  375. * release_open_intent - free up open intent resources
  376. * @nd: pointer to nameidata
  377. */
  378. void release_open_intent(struct nameidata *nd)
  379. {
  380. struct file *file = nd->intent.open.file;
  381. if (file && !IS_ERR(file)) {
  382. if (file->f_path.dentry == NULL)
  383. put_filp(file);
  384. else
  385. fput(file);
  386. }
  387. }
  388. static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
  389. {
  390. return dentry->d_op->d_revalidate(dentry, nd);
  391. }
  392. /**
  393. * complete_walk - successful completion of path walk
  394. * @nd: pointer nameidata
  395. *
  396. * If we had been in RCU mode, drop out of it and legitimize nd->path.
  397. * Revalidate the final result, unless we'd already done that during
  398. * the path walk or the filesystem doesn't ask for it. Return 0 on
  399. * success, -error on failure. In case of failure caller does not
  400. * need to drop nd->path.
  401. */
  402. static int complete_walk(struct nameidata *nd)
  403. {
  404. struct dentry *dentry = nd->path.dentry;
  405. int status;
  406. if (nd->flags & LOOKUP_RCU) {
  407. nd->flags &= ~LOOKUP_RCU;
  408. if (!(nd->flags & LOOKUP_ROOT))
  409. nd->root.mnt = NULL;
  410. spin_lock(&dentry->d_lock);
  411. if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
  412. spin_unlock(&dentry->d_lock);
  413. rcu_read_unlock();
  414. br_read_unlock(vfsmount_lock);
  415. return -ECHILD;
  416. }
  417. BUG_ON(nd->inode != dentry->d_inode);
  418. spin_unlock(&dentry->d_lock);
  419. mntget(nd->path.mnt);
  420. rcu_read_unlock();
  421. br_read_unlock(vfsmount_lock);
  422. }
  423. if (likely(!(nd->flags & LOOKUP_JUMPED)))
  424. return 0;
  425. if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
  426. return 0;
  427. if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
  428. return 0;
  429. /* Note: we do not d_invalidate() */
  430. status = d_revalidate(dentry, nd);
  431. if (status > 0)
  432. return 0;
  433. if (!status)
  434. status = -ESTALE;
  435. path_put(&nd->path);
  436. return status;
  437. }
  438. static __always_inline void set_root(struct nameidata *nd)
  439. {
  440. if (!nd->root.mnt)
  441. get_fs_root(current->fs, &nd->root);
  442. }
  443. static int link_path_walk(const char *, struct nameidata *);
  444. static __always_inline void set_root_rcu(struct nameidata *nd)
  445. {
  446. if (!nd->root.mnt) {
  447. struct fs_struct *fs = current->fs;
  448. unsigned seq;
  449. do {
  450. seq = read_seqcount_begin(&fs->seq);
  451. nd->root = fs->root;
  452. nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
  453. } while (read_seqcount_retry(&fs->seq, seq));
  454. }
  455. }
  456. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  457. {
  458. int ret;
  459. if (IS_ERR(link))
  460. goto fail;
  461. if (*link == '/') {
  462. set_root(nd);
  463. path_put(&nd->path);
  464. nd->path = nd->root;
  465. path_get(&nd->root);
  466. nd->flags |= LOOKUP_JUMPED;
  467. }
  468. nd->inode = nd->path.dentry->d_inode;
  469. ret = link_path_walk(link, nd);
  470. return ret;
  471. fail:
  472. path_put(&nd->path);
  473. return PTR_ERR(link);
  474. }
  475. static void path_put_conditional(struct path *path, struct nameidata *nd)
  476. {
  477. dput(path->dentry);
  478. if (path->mnt != nd->path.mnt)
  479. mntput(path->mnt);
  480. }
  481. static inline void path_to_nameidata(const struct path *path,
  482. struct nameidata *nd)
  483. {
  484. if (!(nd->flags & LOOKUP_RCU)) {
  485. dput(nd->path.dentry);
  486. if (nd->path.mnt != path->mnt)
  487. mntput(nd->path.mnt);
  488. }
  489. nd->path.mnt = path->mnt;
  490. nd->path.dentry = path->dentry;
  491. }
  492. static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
  493. {
  494. struct inode *inode = link->dentry->d_inode;
  495. if (!IS_ERR(cookie) && inode->i_op->put_link)
  496. inode->i_op->put_link(link->dentry, nd, cookie);
  497. path_put(link);
  498. }
  499. static __always_inline int
  500. follow_link(struct path *link, struct nameidata *nd, void **p)
  501. {
  502. int error;
  503. struct dentry *dentry = link->dentry;
  504. BUG_ON(nd->flags & LOOKUP_RCU);
  505. if (link->mnt == nd->path.mnt)
  506. mntget(link->mnt);
  507. if (unlikely(current->total_link_count >= 40)) {
  508. *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
  509. path_put(&nd->path);
  510. return -ELOOP;
  511. }
  512. cond_resched();
  513. current->total_link_count++;
  514. touch_atime(link->mnt, dentry);
  515. nd_set_link(nd, NULL);
  516. error = security_inode_follow_link(link->dentry, nd);
  517. if (error) {
  518. *p = ERR_PTR(error); /* no ->put_link(), please */
  519. path_put(&nd->path);
  520. return error;
  521. }
  522. nd->last_type = LAST_BIND;
  523. *p = dentry->d_inode->i_op->follow_link(dentry, nd);
  524. error = PTR_ERR(*p);
  525. if (!IS_ERR(*p)) {
  526. char *s = nd_get_link(nd);
  527. error = 0;
  528. if (s)
  529. error = __vfs_follow_link(nd, s);
  530. else if (nd->last_type == LAST_BIND) {
  531. nd->flags |= LOOKUP_JUMPED;
  532. nd->inode = nd->path.dentry->d_inode;
  533. if (nd->inode->i_op->follow_link) {
  534. /* stepped on a _really_ weird one */
  535. path_put(&nd->path);
  536. error = -ELOOP;
  537. }
  538. }
  539. }
  540. return error;
  541. }
  542. static int follow_up_rcu(struct path *path)
  543. {
  544. struct vfsmount *parent;
  545. struct dentry *mountpoint;
  546. parent = path->mnt->mnt_parent;
  547. if (parent == path->mnt)
  548. return 0;
  549. mountpoint = path->mnt->mnt_mountpoint;
  550. path->dentry = mountpoint;
  551. path->mnt = parent;
  552. return 1;
  553. }
  554. int follow_up(struct path *path)
  555. {
  556. struct vfsmount *parent;
  557. struct dentry *mountpoint;
  558. br_read_lock(vfsmount_lock);
  559. parent = path->mnt->mnt_parent;
  560. if (parent == path->mnt) {
  561. br_read_unlock(vfsmount_lock);
  562. return 0;
  563. }
  564. mntget(parent);
  565. mountpoint = dget(path->mnt->mnt_mountpoint);
  566. br_read_unlock(vfsmount_lock);
  567. dput(path->dentry);
  568. path->dentry = mountpoint;
  569. mntput(path->mnt);
  570. path->mnt = parent;
  571. return 1;
  572. }
  573. /*
  574. * Perform an automount
  575. * - return -EISDIR to tell follow_managed() to stop and return the path we
  576. * were called with.
  577. */
  578. static int follow_automount(struct path *path, unsigned flags,
  579. bool *need_mntput)
  580. {
  581. struct vfsmount *mnt;
  582. int err;
  583. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  584. return -EREMOTE;
  585. /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
  586. * and this is the terminal part of the path.
  587. */
  588. if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_PARENT))
  589. return -EISDIR; /* we actually want to stop here */
  590. /* We want to mount if someone is trying to open/create a file of any
  591. * type under the mountpoint, wants to traverse through the mountpoint
  592. * or wants to open the mounted directory.
  593. *
  594. * We don't want to mount if someone's just doing a stat and they've
  595. * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
  596. * appended a '/' to the name.
  597. */
  598. if (!(flags & LOOKUP_FOLLOW) &&
  599. !(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
  600. LOOKUP_OPEN | LOOKUP_CREATE)))
  601. return -EISDIR;
  602. current->total_link_count++;
  603. if (current->total_link_count >= 40)
  604. return -ELOOP;
  605. mnt = path->dentry->d_op->d_automount(path);
  606. if (IS_ERR(mnt)) {
  607. /*
  608. * The filesystem is allowed to return -EISDIR here to indicate
  609. * it doesn't want to automount. For instance, autofs would do
  610. * this so that its userspace daemon can mount on this dentry.
  611. *
  612. * However, we can only permit this if it's a terminal point in
  613. * the path being looked up; if it wasn't then the remainder of
  614. * the path is inaccessible and we should say so.
  615. */
  616. if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
  617. return -EREMOTE;
  618. return PTR_ERR(mnt);
  619. }
  620. if (!mnt) /* mount collision */
  621. return 0;
  622. if (!*need_mntput) {
  623. /* lock_mount() may release path->mnt on error */
  624. mntget(path->mnt);
  625. *need_mntput = true;
  626. }
  627. err = finish_automount(mnt, path);
  628. switch (err) {
  629. case -EBUSY:
  630. /* Someone else made a mount here whilst we were busy */
  631. return 0;
  632. case 0:
  633. path_put(path);
  634. path->mnt = mnt;
  635. path->dentry = dget(mnt->mnt_root);
  636. return 0;
  637. default:
  638. return err;
  639. }
  640. }
  641. /*
  642. * Handle a dentry that is managed in some way.
  643. * - Flagged for transit management (autofs)
  644. * - Flagged as mountpoint
  645. * - Flagged as automount point
  646. *
  647. * This may only be called in refwalk mode.
  648. *
  649. * Serialization is taken care of in namespace.c
  650. */
  651. static int follow_managed(struct path *path, unsigned flags)
  652. {
  653. struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
  654. unsigned managed;
  655. bool need_mntput = false;
  656. int ret = 0;
  657. /* Given that we're not holding a lock here, we retain the value in a
  658. * local variable for each dentry as we look at it so that we don't see
  659. * the components of that value change under us */
  660. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  661. managed &= DCACHE_MANAGED_DENTRY,
  662. unlikely(managed != 0)) {
  663. /* Allow the filesystem to manage the transit without i_mutex
  664. * being held. */
  665. if (managed & DCACHE_MANAGE_TRANSIT) {
  666. BUG_ON(!path->dentry->d_op);
  667. BUG_ON(!path->dentry->d_op->d_manage);
  668. ret = path->dentry->d_op->d_manage(path->dentry, false);
  669. if (ret < 0)
  670. break;
  671. }
  672. /* Transit to a mounted filesystem. */
  673. if (managed & DCACHE_MOUNTED) {
  674. struct vfsmount *mounted = lookup_mnt(path);
  675. if (mounted) {
  676. dput(path->dentry);
  677. if (need_mntput)
  678. mntput(path->mnt);
  679. path->mnt = mounted;
  680. path->dentry = dget(mounted->mnt_root);
  681. need_mntput = true;
  682. continue;
  683. }
  684. /* Something is mounted on this dentry in another
  685. * namespace and/or whatever was mounted there in this
  686. * namespace got unmounted before we managed to get the
  687. * vfsmount_lock */
  688. }
  689. /* Handle an automount point */
  690. if (managed & DCACHE_NEED_AUTOMOUNT) {
  691. ret = follow_automount(path, flags, &need_mntput);
  692. if (ret < 0)
  693. break;
  694. continue;
  695. }
  696. /* We didn't change the current path point */
  697. break;
  698. }
  699. if (need_mntput && path->mnt == mnt)
  700. mntput(path->mnt);
  701. if (ret == -EISDIR)
  702. ret = 0;
  703. return ret;
  704. }
  705. int follow_down_one(struct path *path)
  706. {
  707. struct vfsmount *mounted;
  708. mounted = lookup_mnt(path);
  709. if (mounted) {
  710. dput(path->dentry);
  711. mntput(path->mnt);
  712. path->mnt = mounted;
  713. path->dentry = dget(mounted->mnt_root);
  714. return 1;
  715. }
  716. return 0;
  717. }
  718. static inline bool managed_dentry_might_block(struct dentry *dentry)
  719. {
  720. return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
  721. dentry->d_op->d_manage(dentry, true) < 0);
  722. }
  723. /*
  724. * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
  725. * we meet a managed dentry that would need blocking.
  726. */
  727. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  728. struct inode **inode)
  729. {
  730. for (;;) {
  731. struct vfsmount *mounted;
  732. /*
  733. * Don't forget we might have a non-mountpoint managed dentry
  734. * that wants to block transit.
  735. */
  736. if (unlikely(managed_dentry_might_block(path->dentry)))
  737. return false;
  738. if (!d_mountpoint(path->dentry))
  739. break;
  740. mounted = __lookup_mnt(path->mnt, path->dentry, 1);
  741. if (!mounted)
  742. break;
  743. path->mnt = mounted;
  744. path->dentry = mounted->mnt_root;
  745. nd->seq = read_seqcount_begin(&path->dentry->d_seq);
  746. /*
  747. * Update the inode too. We don't need to re-check the
  748. * dentry sequence number here after this d_inode read,
  749. * because a mount-point is always pinned.
  750. */
  751. *inode = path->dentry->d_inode;
  752. }
  753. return true;
  754. }
  755. static void follow_mount_rcu(struct nameidata *nd)
  756. {
  757. while (d_mountpoint(nd->path.dentry)) {
  758. struct vfsmount *mounted;
  759. mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
  760. if (!mounted)
  761. break;
  762. nd->path.mnt = mounted;
  763. nd->path.dentry = mounted->mnt_root;
  764. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  765. }
  766. }
  767. static int follow_dotdot_rcu(struct nameidata *nd)
  768. {
  769. set_root_rcu(nd);
  770. while (1) {
  771. if (nd->path.dentry == nd->root.dentry &&
  772. nd->path.mnt == nd->root.mnt) {
  773. break;
  774. }
  775. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  776. struct dentry *old = nd->path.dentry;
  777. struct dentry *parent = old->d_parent;
  778. unsigned seq;
  779. seq = read_seqcount_begin(&parent->d_seq);
  780. if (read_seqcount_retry(&old->d_seq, nd->seq))
  781. goto failed;
  782. nd->path.dentry = parent;
  783. nd->seq = seq;
  784. break;
  785. }
  786. if (!follow_up_rcu(&nd->path))
  787. break;
  788. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  789. }
  790. follow_mount_rcu(nd);
  791. nd->inode = nd->path.dentry->d_inode;
  792. return 0;
  793. failed:
  794. nd->flags &= ~LOOKUP_RCU;
  795. if (!(nd->flags & LOOKUP_ROOT))
  796. nd->root.mnt = NULL;
  797. rcu_read_unlock();
  798. br_read_unlock(vfsmount_lock);
  799. return -ECHILD;
  800. }
  801. /*
  802. * Follow down to the covering mount currently visible to userspace. At each
  803. * point, the filesystem owning that dentry may be queried as to whether the
  804. * caller is permitted to proceed or not.
  805. */
  806. int follow_down(struct path *path)
  807. {
  808. unsigned managed;
  809. int ret;
  810. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  811. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  812. /* Allow the filesystem to manage the transit without i_mutex
  813. * being held.
  814. *
  815. * We indicate to the filesystem if someone is trying to mount
  816. * something here. This gives autofs the chance to deny anyone
  817. * other than its daemon the right to mount on its
  818. * superstructure.
  819. *
  820. * The filesystem may sleep at this point.
  821. */
  822. if (managed & DCACHE_MANAGE_TRANSIT) {
  823. BUG_ON(!path->dentry->d_op);
  824. BUG_ON(!path->dentry->d_op->d_manage);
  825. ret = path->dentry->d_op->d_manage(
  826. path->dentry, false);
  827. if (ret < 0)
  828. return ret == -EISDIR ? 0 : ret;
  829. }
  830. /* Transit to a mounted filesystem. */
  831. if (managed & DCACHE_MOUNTED) {
  832. struct vfsmount *mounted = lookup_mnt(path);
  833. if (!mounted)
  834. break;
  835. dput(path->dentry);
  836. mntput(path->mnt);
  837. path->mnt = mounted;
  838. path->dentry = dget(mounted->mnt_root);
  839. continue;
  840. }
  841. /* Don't handle automount points here */
  842. break;
  843. }
  844. return 0;
  845. }
  846. /*
  847. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  848. */
  849. static void follow_mount(struct path *path)
  850. {
  851. while (d_mountpoint(path->dentry)) {
  852. struct vfsmount *mounted = lookup_mnt(path);
  853. if (!mounted)
  854. break;
  855. dput(path->dentry);
  856. mntput(path->mnt);
  857. path->mnt = mounted;
  858. path->dentry = dget(mounted->mnt_root);
  859. }
  860. }
  861. static void follow_dotdot(struct nameidata *nd)
  862. {
  863. set_root(nd);
  864. while(1) {
  865. struct dentry *old = nd->path.dentry;
  866. if (nd->path.dentry == nd->root.dentry &&
  867. nd->path.mnt == nd->root.mnt) {
  868. break;
  869. }
  870. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  871. /* rare case of legitimate dget_parent()... */
  872. nd->path.dentry = dget_parent(nd->path.dentry);
  873. dput(old);
  874. break;
  875. }
  876. if (!follow_up(&nd->path))
  877. break;
  878. }
  879. follow_mount(&nd->path);
  880. nd->inode = nd->path.dentry->d_inode;
  881. }
  882. /*
  883. * Allocate a dentry with name and parent, and perform a parent
  884. * directory ->lookup on it. Returns the new dentry, or ERR_PTR
  885. * on error. parent->d_inode->i_mutex must be held. d_lookup must
  886. * have verified that no child exists while under i_mutex.
  887. */
  888. static struct dentry *d_alloc_and_lookup(struct dentry *parent,
  889. struct qstr *name, struct nameidata *nd)
  890. {
  891. struct inode *inode = parent->d_inode;
  892. struct dentry *dentry;
  893. struct dentry *old;
  894. /* Don't create child dentry for a dead directory. */
  895. if (unlikely(IS_DEADDIR(inode)))
  896. return ERR_PTR(-ENOENT);
  897. dentry = d_alloc(parent, name);
  898. if (unlikely(!dentry))
  899. return ERR_PTR(-ENOMEM);
  900. old = inode->i_op->lookup(inode, dentry, nd);
  901. if (unlikely(old)) {
  902. dput(dentry);
  903. dentry = old;
  904. }
  905. return dentry;
  906. }
  907. /*
  908. * We already have a dentry, but require a lookup to be performed on the parent
  909. * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
  910. * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
  911. * child exists while under i_mutex.
  912. */
  913. static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
  914. struct nameidata *nd)
  915. {
  916. struct inode *inode = parent->d_inode;
  917. struct dentry *old;
  918. /* Don't create child dentry for a dead directory. */
  919. if (unlikely(IS_DEADDIR(inode)))
  920. return ERR_PTR(-ENOENT);
  921. old = inode->i_op->lookup(inode, dentry, nd);
  922. if (unlikely(old)) {
  923. dput(dentry);
  924. dentry = old;
  925. }
  926. return dentry;
  927. }
  928. /*
  929. * It's more convoluted than I'd like it to be, but... it's still fairly
  930. * small and for now I'd prefer to have fast path as straight as possible.
  931. * It _is_ time-critical.
  932. */
  933. static int do_lookup(struct nameidata *nd, struct qstr *name,
  934. struct path *path, struct inode **inode)
  935. {
  936. struct vfsmount *mnt = nd->path.mnt;
  937. struct dentry *dentry, *parent = nd->path.dentry;
  938. int need_reval = 1;
  939. int status = 1;
  940. int err;
  941. /*
  942. * Rename seqlock is not required here because in the off chance
  943. * of a false negative due to a concurrent rename, we're going to
  944. * do the non-racy lookup, below.
  945. */
  946. if (nd->flags & LOOKUP_RCU) {
  947. unsigned seq;
  948. *inode = nd->inode;
  949. dentry = __d_lookup_rcu(parent, name, &seq, inode);
  950. if (!dentry)
  951. goto unlazy;
  952. /* Memory barrier in read_seqcount_begin of child is enough */
  953. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  954. return -ECHILD;
  955. nd->seq = seq;
  956. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  957. status = d_revalidate(dentry, nd);
  958. if (unlikely(status <= 0)) {
  959. if (status != -ECHILD)
  960. need_reval = 0;
  961. goto unlazy;
  962. }
  963. }
  964. if (unlikely(d_need_lookup(dentry)))
  965. goto unlazy;
  966. path->mnt = mnt;
  967. path->dentry = dentry;
  968. if (unlikely(!__follow_mount_rcu(nd, path, inode)))
  969. goto unlazy;
  970. if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  971. goto unlazy;
  972. return 0;
  973. unlazy:
  974. if (unlazy_walk(nd, dentry))
  975. return -ECHILD;
  976. } else {
  977. dentry = __d_lookup(parent, name);
  978. }
  979. if (dentry && unlikely(d_need_lookup(dentry))) {
  980. dput(dentry);
  981. dentry = NULL;
  982. }
  983. retry:
  984. if (unlikely(!dentry)) {
  985. struct inode *dir = parent->d_inode;
  986. BUG_ON(nd->inode != dir);
  987. mutex_lock(&dir->i_mutex);
  988. dentry = d_lookup(parent, name);
  989. if (likely(!dentry)) {
  990. dentry = d_alloc_and_lookup(parent, name, nd);
  991. if (IS_ERR(dentry)) {
  992. mutex_unlock(&dir->i_mutex);
  993. return PTR_ERR(dentry);
  994. }
  995. /* known good */
  996. need_reval = 0;
  997. status = 1;
  998. } else if (unlikely(d_need_lookup(dentry))) {
  999. dentry = d_inode_lookup(parent, dentry, nd);
  1000. if (IS_ERR(dentry)) {
  1001. mutex_unlock(&dir->i_mutex);
  1002. return PTR_ERR(dentry);
  1003. }
  1004. /* known good */
  1005. need_reval = 0;
  1006. status = 1;
  1007. }
  1008. mutex_unlock(&dir->i_mutex);
  1009. }
  1010. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1011. status = d_revalidate(dentry, nd);
  1012. if (unlikely(status <= 0)) {
  1013. if (status < 0) {
  1014. dput(dentry);
  1015. return status;
  1016. }
  1017. if (!d_invalidate(dentry)) {
  1018. dput(dentry);
  1019. dentry = NULL;
  1020. need_reval = 1;
  1021. goto retry;
  1022. }
  1023. }
  1024. path->mnt = mnt;
  1025. path->dentry = dentry;
  1026. err = follow_managed(path, nd->flags);
  1027. if (unlikely(err < 0)) {
  1028. path_put_conditional(path, nd);
  1029. return err;
  1030. }
  1031. *inode = path->dentry->d_inode;
  1032. return 0;
  1033. }
  1034. static inline int may_lookup(struct nameidata *nd)
  1035. {
  1036. if (nd->flags & LOOKUP_RCU) {
  1037. int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
  1038. if (err != -ECHILD)
  1039. return err;
  1040. if (unlazy_walk(nd, NULL))
  1041. return -ECHILD;
  1042. }
  1043. return inode_permission(nd->inode, MAY_EXEC);
  1044. }
  1045. static inline int handle_dots(struct nameidata *nd, int type)
  1046. {
  1047. if (type == LAST_DOTDOT) {
  1048. if (nd->flags & LOOKUP_RCU) {
  1049. if (follow_dotdot_rcu(nd))
  1050. return -ECHILD;
  1051. } else
  1052. follow_dotdot(nd);
  1053. }
  1054. return 0;
  1055. }
  1056. static void terminate_walk(struct nameidata *nd)
  1057. {
  1058. if (!(nd->flags & LOOKUP_RCU)) {
  1059. path_put(&nd->path);
  1060. } else {
  1061. nd->flags &= ~LOOKUP_RCU;
  1062. if (!(nd->flags & LOOKUP_ROOT))
  1063. nd->root.mnt = NULL;
  1064. rcu_read_unlock();
  1065. br_read_unlock(vfsmount_lock);
  1066. }
  1067. }
  1068. static inline int walk_component(struct nameidata *nd, struct path *path,
  1069. struct qstr *name, int type, int follow)
  1070. {
  1071. struct inode *inode;
  1072. int err;
  1073. /*
  1074. * "." and ".." are special - ".." especially so because it has
  1075. * to be able to know about the current root directory and
  1076. * parent relationships.
  1077. */
  1078. if (unlikely(type != LAST_NORM))
  1079. return handle_dots(nd, type);
  1080. err = do_lookup(nd, name, path, &inode);
  1081. if (unlikely(err)) {
  1082. terminate_walk(nd);
  1083. return err;
  1084. }
  1085. if (!inode) {
  1086. path_to_nameidata(path, nd);
  1087. terminate_walk(nd);
  1088. return -ENOENT;
  1089. }
  1090. if (unlikely(inode->i_op->follow_link) && follow) {
  1091. if (nd->flags & LOOKUP_RCU) {
  1092. if (unlikely(unlazy_walk(nd, path->dentry))) {
  1093. terminate_walk(nd);
  1094. return -ECHILD;
  1095. }
  1096. }
  1097. BUG_ON(inode != path->dentry->d_inode);
  1098. return 1;
  1099. }
  1100. path_to_nameidata(path, nd);
  1101. nd->inode = inode;
  1102. return 0;
  1103. }
  1104. /*
  1105. * This limits recursive symlink follows to 8, while
  1106. * limiting consecutive symlinks to 40.
  1107. *
  1108. * Without that kind of total limit, nasty chains of consecutive
  1109. * symlinks can cause almost arbitrarily long lookups.
  1110. */
  1111. static inline int nested_symlink(struct path *path, struct nameidata *nd)
  1112. {
  1113. int res;
  1114. if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
  1115. path_put_conditional(path, nd);
  1116. path_put(&nd->path);
  1117. return -ELOOP;
  1118. }
  1119. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  1120. nd->depth++;
  1121. current->link_count++;
  1122. do {
  1123. struct path link = *path;
  1124. void *cookie;
  1125. res = follow_link(&link, nd, &cookie);
  1126. if (!res)
  1127. res = walk_component(nd, path, &nd->last,
  1128. nd->last_type, LOOKUP_FOLLOW);
  1129. put_link(nd, &link, cookie);
  1130. } while (res > 0);
  1131. current->link_count--;
  1132. nd->depth--;
  1133. return res;
  1134. }
  1135. /*
  1136. * Name resolution.
  1137. * This is the basic name resolution function, turning a pathname into
  1138. * the final dentry. We expect 'base' to be positive and a directory.
  1139. *
  1140. * Returns 0 and nd will have valid dentry and mnt on success.
  1141. * Returns error and drops reference to input namei data on failure.
  1142. */
  1143. static int link_path_walk(const char *name, struct nameidata *nd)
  1144. {
  1145. struct path next;
  1146. int err;
  1147. while (*name=='/')
  1148. name++;
  1149. if (!*name)
  1150. return 0;
  1151. /* At this point we know we have a real path component. */
  1152. for(;;) {
  1153. unsigned long hash;
  1154. struct qstr this;
  1155. unsigned int c;
  1156. int type;
  1157. err = may_lookup(nd);
  1158. if (err)
  1159. break;
  1160. this.name = name;
  1161. c = *(const unsigned char *)name;
  1162. hash = init_name_hash();
  1163. do {
  1164. name++;
  1165. hash = partial_name_hash(c, hash);
  1166. c = *(const unsigned char *)name;
  1167. } while (c && (c != '/'));
  1168. this.len = name - (const char *) this.name;
  1169. this.hash = end_name_hash(hash);
  1170. type = LAST_NORM;
  1171. if (this.name[0] == '.') switch (this.len) {
  1172. case 2:
  1173. if (this.name[1] == '.') {
  1174. type = LAST_DOTDOT;
  1175. nd->flags |= LOOKUP_JUMPED;
  1176. }
  1177. break;
  1178. case 1:
  1179. type = LAST_DOT;
  1180. }
  1181. if (likely(type == LAST_NORM)) {
  1182. struct dentry *parent = nd->path.dentry;
  1183. nd->flags &= ~LOOKUP_JUMPED;
  1184. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1185. err = parent->d_op->d_hash(parent, nd->inode,
  1186. &this);
  1187. if (err < 0)
  1188. break;
  1189. }
  1190. }
  1191. /* remove trailing slashes? */
  1192. if (!c)
  1193. goto last_component;
  1194. while (*++name == '/');
  1195. if (!*name)
  1196. goto last_component;
  1197. err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
  1198. if (err < 0)
  1199. return err;
  1200. if (err) {
  1201. err = nested_symlink(&next, nd);
  1202. if (err)
  1203. return err;
  1204. }
  1205. err = -ENOTDIR;
  1206. if (!nd->inode->i_op->lookup)
  1207. break;
  1208. continue;
  1209. /* here ends the main loop */
  1210. last_component:
  1211. nd->last = this;
  1212. nd->last_type = type;
  1213. return 0;
  1214. }
  1215. terminate_walk(nd);
  1216. return err;
  1217. }
  1218. static int path_init(int dfd, const char *name, unsigned int flags,
  1219. struct nameidata *nd, struct file **fp)
  1220. {
  1221. int retval = 0;
  1222. int fput_needed;
  1223. struct file *file;
  1224. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1225. nd->flags = flags | LOOKUP_JUMPED;
  1226. nd->depth = 0;
  1227. if (flags & LOOKUP_ROOT) {
  1228. struct inode *inode = nd->root.dentry->d_inode;
  1229. if (*name) {
  1230. if (!inode->i_op->lookup)
  1231. return -ENOTDIR;
  1232. retval = inode_permission(inode, MAY_EXEC);
  1233. if (retval)
  1234. return retval;
  1235. }
  1236. nd->path = nd->root;
  1237. nd->inode = inode;
  1238. if (flags & LOOKUP_RCU) {
  1239. br_read_lock(vfsmount_lock);
  1240. rcu_read_lock();
  1241. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1242. } else {
  1243. path_get(&nd->path);
  1244. }
  1245. return 0;
  1246. }
  1247. nd->root.mnt = NULL;
  1248. if (*name=='/') {
  1249. if (flags & LOOKUP_RCU) {
  1250. br_read_lock(vfsmount_lock);
  1251. rcu_read_lock();
  1252. set_root_rcu(nd);
  1253. } else {
  1254. set_root(nd);
  1255. path_get(&nd->root);
  1256. }
  1257. nd->path = nd->root;
  1258. } else if (dfd == AT_FDCWD) {
  1259. if (flags & LOOKUP_RCU) {
  1260. struct fs_struct *fs = current->fs;
  1261. unsigned seq;
  1262. br_read_lock(vfsmount_lock);
  1263. rcu_read_lock();
  1264. do {
  1265. seq = read_seqcount_begin(&fs->seq);
  1266. nd->path = fs->pwd;
  1267. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1268. } while (read_seqcount_retry(&fs->seq, seq));
  1269. } else {
  1270. get_fs_pwd(current->fs, &nd->path);
  1271. }
  1272. } else {
  1273. struct dentry *dentry;
  1274. file = fget_raw_light(dfd, &fput_needed);
  1275. retval = -EBADF;
  1276. if (!file)
  1277. goto out_fail;
  1278. dentry = file->f_path.dentry;
  1279. if (*name) {
  1280. retval = -ENOTDIR;
  1281. if (!S_ISDIR(dentry->d_inode->i_mode))
  1282. goto fput_fail;
  1283. retval = inode_permission(dentry->d_inode, MAY_EXEC);
  1284. if (retval)
  1285. goto fput_fail;
  1286. }
  1287. nd->path = file->f_path;
  1288. if (flags & LOOKUP_RCU) {
  1289. if (fput_needed)
  1290. *fp = file;
  1291. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1292. br_read_lock(vfsmount_lock);
  1293. rcu_read_lock();
  1294. } else {
  1295. path_get(&file->f_path);
  1296. fput_light(file, fput_needed);
  1297. }
  1298. }
  1299. nd->inode = nd->path.dentry->d_inode;
  1300. return 0;
  1301. fput_fail:
  1302. fput_light(file, fput_needed);
  1303. out_fail:
  1304. return retval;
  1305. }
  1306. static inline int lookup_last(struct nameidata *nd, struct path *path)
  1307. {
  1308. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1309. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1310. nd->flags &= ~LOOKUP_PARENT;
  1311. return walk_component(nd, path, &nd->last, nd->last_type,
  1312. nd->flags & LOOKUP_FOLLOW);
  1313. }
  1314. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1315. static int path_lookupat(int dfd, const char *name,
  1316. unsigned int flags, struct nameidata *nd)
  1317. {
  1318. struct file *base = NULL;
  1319. struct path path;
  1320. int err;
  1321. /*
  1322. * Path walking is largely split up into 2 different synchronisation
  1323. * schemes, rcu-walk and ref-walk (explained in
  1324. * Documentation/filesystems/path-lookup.txt). These share much of the
  1325. * path walk code, but some things particularly setup, cleanup, and
  1326. * following mounts are sufficiently divergent that functions are
  1327. * duplicated. Typically there is a function foo(), and its RCU
  1328. * analogue, foo_rcu().
  1329. *
  1330. * -ECHILD is the error number of choice (just to avoid clashes) that
  1331. * is returned if some aspect of an rcu-walk fails. Such an error must
  1332. * be handled by restarting a traditional ref-walk (which will always
  1333. * be able to complete).
  1334. */
  1335. err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
  1336. if (unlikely(err))
  1337. return err;
  1338. current->total_link_count = 0;
  1339. err = link_path_walk(name, nd);
  1340. if (!err && !(flags & LOOKUP_PARENT)) {
  1341. err = lookup_last(nd, &path);
  1342. while (err > 0) {
  1343. void *cookie;
  1344. struct path link = path;
  1345. nd->flags |= LOOKUP_PARENT;
  1346. err = follow_link(&link, nd, &cookie);
  1347. if (!err)
  1348. err = lookup_last(nd, &path);
  1349. put_link(nd, &link, cookie);
  1350. }
  1351. }
  1352. if (!err)
  1353. err = complete_walk(nd);
  1354. if (!err && nd->flags & LOOKUP_DIRECTORY) {
  1355. if (!nd->inode->i_op->lookup) {
  1356. path_put(&nd->path);
  1357. err = -ENOTDIR;
  1358. }
  1359. }
  1360. if (base)
  1361. fput(base);
  1362. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1363. path_put(&nd->root);
  1364. nd->root.mnt = NULL;
  1365. }
  1366. return err;
  1367. }
  1368. static int do_path_lookup(int dfd, const char *name,
  1369. unsigned int flags, struct nameidata *nd)
  1370. {
  1371. int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  1372. if (unlikely(retval == -ECHILD))
  1373. retval = path_lookupat(dfd, name, flags, nd);
  1374. if (unlikely(retval == -ESTALE))
  1375. retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
  1376. if (likely(!retval)) {
  1377. if (unlikely(!audit_dummy_context())) {
  1378. if (nd->path.dentry && nd->inode)
  1379. audit_inode(name, nd->path.dentry);
  1380. }
  1381. }
  1382. return retval;
  1383. }
  1384. int kern_path_parent(const char *name, struct nameidata *nd)
  1385. {
  1386. return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
  1387. }
  1388. int kern_path(const char *name, unsigned int flags, struct path *path)
  1389. {
  1390. struct nameidata nd;
  1391. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1392. if (!res)
  1393. *path = nd.path;
  1394. return res;
  1395. }
  1396. /**
  1397. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1398. * @dentry: pointer to dentry of the base directory
  1399. * @mnt: pointer to vfs mount of the base directory
  1400. * @name: pointer to file name
  1401. * @flags: lookup flags
  1402. * @path: pointer to struct path to fill
  1403. */
  1404. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1405. const char *name, unsigned int flags,
  1406. struct path *path)
  1407. {
  1408. struct nameidata nd;
  1409. int err;
  1410. nd.root.dentry = dentry;
  1411. nd.root.mnt = mnt;
  1412. BUG_ON(flags & LOOKUP_PARENT);
  1413. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1414. err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
  1415. if (!err)
  1416. *path = nd.path;
  1417. return err;
  1418. }
  1419. static struct dentry *__lookup_hash(struct qstr *name,
  1420. struct dentry *base, struct nameidata *nd)
  1421. {
  1422. struct inode *inode = base->d_inode;
  1423. struct dentry *dentry;
  1424. int err;
  1425. err = inode_permission(inode, MAY_EXEC);
  1426. if (err)
  1427. return ERR_PTR(err);
  1428. /*
  1429. * Don't bother with __d_lookup: callers are for creat as
  1430. * well as unlink, so a lot of the time it would cost
  1431. * a double lookup.
  1432. */
  1433. dentry = d_lookup(base, name);
  1434. if (dentry && d_need_lookup(dentry)) {
  1435. /*
  1436. * __lookup_hash is called with the parent dir's i_mutex already
  1437. * held, so we are good to go here.
  1438. */
  1439. dentry = d_inode_lookup(base, dentry, nd);
  1440. if (IS_ERR(dentry))
  1441. return dentry;
  1442. }
  1443. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1444. int status = d_revalidate(dentry, nd);
  1445. if (unlikely(status <= 0)) {
  1446. /*
  1447. * The dentry failed validation.
  1448. * If d_revalidate returned 0 attempt to invalidate
  1449. * the dentry otherwise d_revalidate is asking us
  1450. * to return a fail status.
  1451. */
  1452. if (status < 0) {
  1453. dput(dentry);
  1454. return ERR_PTR(status);
  1455. } else if (!d_invalidate(dentry)) {
  1456. dput(dentry);
  1457. dentry = NULL;
  1458. }
  1459. }
  1460. }
  1461. if (!dentry)
  1462. dentry = d_alloc_and_lookup(base, name, nd);
  1463. return dentry;
  1464. }
  1465. /*
  1466. * Restricted form of lookup. Doesn't follow links, single-component only,
  1467. * needs parent already locked. Doesn't follow mounts.
  1468. * SMP-safe.
  1469. */
  1470. static struct dentry *lookup_hash(struct nameidata *nd)
  1471. {
  1472. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1473. }
  1474. /**
  1475. * lookup_one_len - filesystem helper to lookup single pathname component
  1476. * @name: pathname component to lookup
  1477. * @base: base directory to lookup from
  1478. * @len: maximum length @len should be interpreted to
  1479. *
  1480. * Note that this routine is purely a helper for filesystem usage and should
  1481. * not be called by generic code. Also note that by using this function the
  1482. * nameidata argument is passed to the filesystem methods and a filesystem
  1483. * using this helper needs to be prepared for that.
  1484. */
  1485. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1486. {
  1487. struct qstr this;
  1488. unsigned long hash;
  1489. unsigned int c;
  1490. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1491. this.name = name;
  1492. this.len = len;
  1493. if (!len)
  1494. return ERR_PTR(-EACCES);
  1495. hash = init_name_hash();
  1496. while (len--) {
  1497. c = *(const unsigned char *)name++;
  1498. if (c == '/' || c == '\0')
  1499. return ERR_PTR(-EACCES);
  1500. hash = partial_name_hash(c, hash);
  1501. }
  1502. this.hash = end_name_hash(hash);
  1503. /*
  1504. * See if the low-level filesystem might want
  1505. * to use its own hash..
  1506. */
  1507. if (base->d_flags & DCACHE_OP_HASH) {
  1508. int err = base->d_op->d_hash(base, base->d_inode, &this);
  1509. if (err < 0)
  1510. return ERR_PTR(err);
  1511. }
  1512. return __lookup_hash(&this, base, NULL);
  1513. }
  1514. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1515. struct path *path)
  1516. {
  1517. struct nameidata nd;
  1518. char *tmp = getname_flags(name, flags);
  1519. int err = PTR_ERR(tmp);
  1520. if (!IS_ERR(tmp)) {
  1521. BUG_ON(flags & LOOKUP_PARENT);
  1522. err = do_path_lookup(dfd, tmp, flags, &nd);
  1523. putname(tmp);
  1524. if (!err)
  1525. *path = nd.path;
  1526. }
  1527. return err;
  1528. }
  1529. static int user_path_parent(int dfd, const char __user *path,
  1530. struct nameidata *nd, char **name)
  1531. {
  1532. char *s = getname(path);
  1533. int error;
  1534. if (IS_ERR(s))
  1535. return PTR_ERR(s);
  1536. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1537. if (error)
  1538. putname(s);
  1539. else
  1540. *name = s;
  1541. return error;
  1542. }
  1543. /*
  1544. * It's inline, so penalty for filesystems that don't use sticky bit is
  1545. * minimal.
  1546. */
  1547. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1548. {
  1549. uid_t fsuid = current_fsuid();
  1550. if (!(dir->i_mode & S_ISVTX))
  1551. return 0;
  1552. if (current_user_ns() != inode_userns(inode))
  1553. goto other_userns;
  1554. if (inode->i_uid == fsuid)
  1555. return 0;
  1556. if (dir->i_uid == fsuid)
  1557. return 0;
  1558. other_userns:
  1559. return !ns_capable(inode_userns(inode), CAP_FOWNER);
  1560. }
  1561. /*
  1562. * Check whether we can remove a link victim from directory dir, check
  1563. * whether the type of victim is right.
  1564. * 1. We can't do it if dir is read-only (done in permission())
  1565. * 2. We should have write and exec permissions on dir
  1566. * 3. We can't remove anything from append-only dir
  1567. * 4. We can't do anything with immutable dir (done in permission())
  1568. * 5. If the sticky bit on dir is set we should either
  1569. * a. be owner of dir, or
  1570. * b. be owner of victim, or
  1571. * c. have CAP_FOWNER capability
  1572. * 6. If the victim is append-only or immutable we can't do antyhing with
  1573. * links pointing to it.
  1574. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1575. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1576. * 9. We can't remove a root or mountpoint.
  1577. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1578. * nfs_async_unlink().
  1579. */
  1580. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1581. {
  1582. int error;
  1583. if (!victim->d_inode)
  1584. return -ENOENT;
  1585. BUG_ON(victim->d_parent->d_inode != dir);
  1586. audit_inode_child(victim, dir);
  1587. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1588. if (error)
  1589. return error;
  1590. if (IS_APPEND(dir))
  1591. return -EPERM;
  1592. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1593. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1594. return -EPERM;
  1595. if (isdir) {
  1596. if (!S_ISDIR(victim->d_inode->i_mode))
  1597. return -ENOTDIR;
  1598. if (IS_ROOT(victim))
  1599. return -EBUSY;
  1600. } else if (S_ISDIR(victim->d_inode->i_mode))
  1601. return -EISDIR;
  1602. if (IS_DEADDIR(dir))
  1603. return -ENOENT;
  1604. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1605. return -EBUSY;
  1606. return 0;
  1607. }
  1608. /* Check whether we can create an object with dentry child in directory
  1609. * dir.
  1610. * 1. We can't do it if child already exists (open has special treatment for
  1611. * this case, but since we are inlined it's OK)
  1612. * 2. We can't do it if dir is read-only (done in permission())
  1613. * 3. We should have write and exec permissions on dir
  1614. * 4. We can't do it if dir is immutable (done in permission())
  1615. */
  1616. static inline int may_create(struct inode *dir, struct dentry *child)
  1617. {
  1618. if (child->d_inode)
  1619. return -EEXIST;
  1620. if (IS_DEADDIR(dir))
  1621. return -ENOENT;
  1622. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1623. }
  1624. /*
  1625. * p1 and p2 should be directories on the same fs.
  1626. */
  1627. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1628. {
  1629. struct dentry *p;
  1630. if (p1 == p2) {
  1631. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1632. return NULL;
  1633. }
  1634. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1635. p = d_ancestor(p2, p1);
  1636. if (p) {
  1637. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1638. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1639. return p;
  1640. }
  1641. p = d_ancestor(p1, p2);
  1642. if (p) {
  1643. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1644. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1645. return p;
  1646. }
  1647. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1648. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1649. return NULL;
  1650. }
  1651. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1652. {
  1653. mutex_unlock(&p1->d_inode->i_mutex);
  1654. if (p1 != p2) {
  1655. mutex_unlock(&p2->d_inode->i_mutex);
  1656. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1657. }
  1658. }
  1659. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1660. struct nameidata *nd)
  1661. {
  1662. int error = may_create(dir, dentry);
  1663. if (error)
  1664. return error;
  1665. if (!dir->i_op->create)
  1666. return -EACCES; /* shouldn't it be ENOSYS? */
  1667. mode &= S_IALLUGO;
  1668. mode |= S_IFREG;
  1669. error = security_inode_create(dir, dentry, mode);
  1670. if (error)
  1671. return error;
  1672. error = dir->i_op->create(dir, dentry, mode, nd);
  1673. if (!error)
  1674. fsnotify_create(dir, dentry);
  1675. return error;
  1676. }
  1677. static int may_open(struct path *path, int acc_mode, int flag)
  1678. {
  1679. struct dentry *dentry = path->dentry;
  1680. struct inode *inode = dentry->d_inode;
  1681. int error;
  1682. /* O_PATH? */
  1683. if (!acc_mode)
  1684. return 0;
  1685. if (!inode)
  1686. return -ENOENT;
  1687. switch (inode->i_mode & S_IFMT) {
  1688. case S_IFLNK:
  1689. return -ELOOP;
  1690. case S_IFDIR:
  1691. if (acc_mode & MAY_WRITE)
  1692. return -EISDIR;
  1693. break;
  1694. case S_IFBLK:
  1695. case S_IFCHR:
  1696. if (path->mnt->mnt_flags & MNT_NODEV)
  1697. return -EACCES;
  1698. /*FALLTHRU*/
  1699. case S_IFIFO:
  1700. case S_IFSOCK:
  1701. flag &= ~O_TRUNC;
  1702. break;
  1703. }
  1704. error = inode_permission(inode, acc_mode);
  1705. if (error)
  1706. return error;
  1707. /*
  1708. * An append-only file must be opened in append mode for writing.
  1709. */
  1710. if (IS_APPEND(inode)) {
  1711. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1712. return -EPERM;
  1713. if (flag & O_TRUNC)
  1714. return -EPERM;
  1715. }
  1716. /* O_NOATIME can only be set by the owner or superuser */
  1717. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  1718. return -EPERM;
  1719. /*
  1720. * Ensure there are no outstanding leases on the file.
  1721. */
  1722. return break_lease(inode, flag);
  1723. }
  1724. static int handle_truncate(struct file *filp)
  1725. {
  1726. struct path *path = &filp->f_path;
  1727. struct inode *inode = path->dentry->d_inode;
  1728. int error = get_write_access(inode);
  1729. if (error)
  1730. return error;
  1731. /*
  1732. * Refuse to truncate files with mandatory locks held on them.
  1733. */
  1734. error = locks_verify_locked(inode);
  1735. if (!error)
  1736. error = security_path_truncate(path);
  1737. if (!error) {
  1738. error = do_truncate(path->dentry, 0,
  1739. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1740. filp);
  1741. }
  1742. put_write_access(inode);
  1743. return error;
  1744. }
  1745. static inline int open_to_namei_flags(int flag)
  1746. {
  1747. if ((flag & O_ACCMODE) == 3)
  1748. flag--;
  1749. return flag;
  1750. }
  1751. /*
  1752. * Handle the last step of open()
  1753. */
  1754. static struct file *do_last(struct nameidata *nd, struct path *path,
  1755. const struct open_flags *op, const char *pathname)
  1756. {
  1757. struct dentry *dir = nd->path.dentry;
  1758. struct dentry *dentry;
  1759. int open_flag = op->open_flag;
  1760. int will_truncate = open_flag & O_TRUNC;
  1761. int want_write = 0;
  1762. int acc_mode = op->acc_mode;
  1763. struct file *filp;
  1764. int error;
  1765. nd->flags &= ~LOOKUP_PARENT;
  1766. nd->flags |= op->intent;
  1767. switch (nd->last_type) {
  1768. case LAST_DOTDOT:
  1769. case LAST_DOT:
  1770. error = handle_dots(nd, nd->last_type);
  1771. if (error)
  1772. return ERR_PTR(error);
  1773. /* fallthrough */
  1774. case LAST_ROOT:
  1775. error = complete_walk(nd);
  1776. if (error)
  1777. return ERR_PTR(error);
  1778. audit_inode(pathname, nd->path.dentry);
  1779. if (open_flag & O_CREAT) {
  1780. error = -EISDIR;
  1781. goto exit;
  1782. }
  1783. goto ok;
  1784. case LAST_BIND:
  1785. error = complete_walk(nd);
  1786. if (error)
  1787. return ERR_PTR(error);
  1788. audit_inode(pathname, dir);
  1789. goto ok;
  1790. }
  1791. if (!(open_flag & O_CREAT)) {
  1792. int symlink_ok = 0;
  1793. if (nd->last.name[nd->last.len])
  1794. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1795. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  1796. symlink_ok = 1;
  1797. /* we _can_ be in RCU mode here */
  1798. error = walk_component(nd, path, &nd->last, LAST_NORM,
  1799. !symlink_ok);
  1800. if (error < 0)
  1801. return ERR_PTR(error);
  1802. if (error) /* symlink */
  1803. return NULL;
  1804. /* sayonara */
  1805. error = complete_walk(nd);
  1806. if (error)
  1807. return ERR_PTR(-ECHILD);
  1808. error = -ENOTDIR;
  1809. if (nd->flags & LOOKUP_DIRECTORY) {
  1810. if (!nd->inode->i_op->lookup)
  1811. goto exit;
  1812. }
  1813. audit_inode(pathname, nd->path.dentry);
  1814. goto ok;
  1815. }
  1816. /* create side of things */
  1817. error = complete_walk(nd);
  1818. if (error)
  1819. return ERR_PTR(error);
  1820. audit_inode(pathname, dir);
  1821. error = -EISDIR;
  1822. /* trailing slashes? */
  1823. if (nd->last.name[nd->last.len])
  1824. goto exit;
  1825. mutex_lock(&dir->d_inode->i_mutex);
  1826. dentry = lookup_hash(nd);
  1827. error = PTR_ERR(dentry);
  1828. if (IS_ERR(dentry)) {
  1829. mutex_unlock(&dir->d_inode->i_mutex);
  1830. goto exit;
  1831. }
  1832. path->dentry = dentry;
  1833. path->mnt = nd->path.mnt;
  1834. /* Negative dentry, just create the file */
  1835. if (!dentry->d_inode) {
  1836. int mode = op->mode;
  1837. if (!IS_POSIXACL(dir->d_inode))
  1838. mode &= ~current_umask();
  1839. /*
  1840. * This write is needed to ensure that a
  1841. * rw->ro transition does not occur between
  1842. * the time when the file is created and when
  1843. * a permanent write count is taken through
  1844. * the 'struct file' in nameidata_to_filp().
  1845. */
  1846. error = mnt_want_write(nd->path.mnt);
  1847. if (error)
  1848. goto exit_mutex_unlock;
  1849. want_write = 1;
  1850. /* Don't check for write permission, don't truncate */
  1851. open_flag &= ~O_TRUNC;
  1852. will_truncate = 0;
  1853. acc_mode = MAY_OPEN;
  1854. error = security_path_mknod(&nd->path, dentry, mode, 0);
  1855. if (error)
  1856. goto exit_mutex_unlock;
  1857. error = vfs_create(dir->d_inode, dentry, mode, nd);
  1858. if (error)
  1859. goto exit_mutex_unlock;
  1860. mutex_unlock(&dir->d_inode->i_mutex);
  1861. dput(nd->path.dentry);
  1862. nd->path.dentry = dentry;
  1863. goto common;
  1864. }
  1865. /*
  1866. * It already exists.
  1867. */
  1868. mutex_unlock(&dir->d_inode->i_mutex);
  1869. audit_inode(pathname, path->dentry);
  1870. error = -EEXIST;
  1871. if (open_flag & O_EXCL)
  1872. goto exit_dput;
  1873. error = follow_managed(path, nd->flags);
  1874. if (error < 0)
  1875. goto exit_dput;
  1876. error = -ENOENT;
  1877. if (!path->dentry->d_inode)
  1878. goto exit_dput;
  1879. if (path->dentry->d_inode->i_op->follow_link)
  1880. return NULL;
  1881. path_to_nameidata(path, nd);
  1882. nd->inode = path->dentry->d_inode;
  1883. error = -EISDIR;
  1884. if (S_ISDIR(nd->inode->i_mode))
  1885. goto exit;
  1886. ok:
  1887. if (!S_ISREG(nd->inode->i_mode))
  1888. will_truncate = 0;
  1889. if (will_truncate) {
  1890. error = mnt_want_write(nd->path.mnt);
  1891. if (error)
  1892. goto exit;
  1893. want_write = 1;
  1894. }
  1895. common:
  1896. error = may_open(&nd->path, acc_mode, open_flag);
  1897. if (error)
  1898. goto exit;
  1899. filp = nameidata_to_filp(nd);
  1900. if (!IS_ERR(filp)) {
  1901. error = ima_file_check(filp, op->acc_mode);
  1902. if (error) {
  1903. fput(filp);
  1904. filp = ERR_PTR(error);
  1905. }
  1906. }
  1907. if (!IS_ERR(filp)) {
  1908. if (will_truncate) {
  1909. error = handle_truncate(filp);
  1910. if (error) {
  1911. fput(filp);
  1912. filp = ERR_PTR(error);
  1913. }
  1914. }
  1915. }
  1916. out:
  1917. if (want_write)
  1918. mnt_drop_write(nd->path.mnt);
  1919. path_put(&nd->path);
  1920. return filp;
  1921. exit_mutex_unlock:
  1922. mutex_unlock(&dir->d_inode->i_mutex);
  1923. exit_dput:
  1924. path_put_conditional(path, nd);
  1925. exit:
  1926. filp = ERR_PTR(error);
  1927. goto out;
  1928. }
  1929. static struct file *path_openat(int dfd, const char *pathname,
  1930. struct nameidata *nd, const struct open_flags *op, int flags)
  1931. {
  1932. struct file *base = NULL;
  1933. struct file *filp;
  1934. struct path path;
  1935. int error;
  1936. filp = get_empty_filp();
  1937. if (!filp)
  1938. return ERR_PTR(-ENFILE);
  1939. filp->f_flags = op->open_flag;
  1940. nd->intent.open.file = filp;
  1941. nd->intent.open.flags = open_to_namei_flags(op->open_flag);
  1942. nd->intent.open.create_mode = op->mode;
  1943. error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
  1944. if (unlikely(error))
  1945. goto out_filp;
  1946. current->total_link_count = 0;
  1947. error = link_path_walk(pathname, nd);
  1948. if (unlikely(error))
  1949. goto out_filp;
  1950. filp = do_last(nd, &path, op, pathname);
  1951. while (unlikely(!filp)) { /* trailing symlink */
  1952. struct path link = path;
  1953. void *cookie;
  1954. if (!(nd->flags & LOOKUP_FOLLOW)) {
  1955. path_put_conditional(&path, nd);
  1956. path_put(&nd->path);
  1957. filp = ERR_PTR(-ELOOP);
  1958. break;
  1959. }
  1960. nd->flags |= LOOKUP_PARENT;
  1961. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  1962. error = follow_link(&link, nd, &cookie);
  1963. if (unlikely(error))
  1964. filp = ERR_PTR(error);
  1965. else
  1966. filp = do_last(nd, &path, op, pathname);
  1967. put_link(nd, &link, cookie);
  1968. }
  1969. out:
  1970. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  1971. path_put(&nd->root);
  1972. if (base)
  1973. fput(base);
  1974. release_open_intent(nd);
  1975. return filp;
  1976. out_filp:
  1977. filp = ERR_PTR(error);
  1978. goto out;
  1979. }
  1980. struct file *do_filp_open(int dfd, const char *pathname,
  1981. const struct open_flags *op, int flags)
  1982. {
  1983. struct nameidata nd;
  1984. struct file *filp;
  1985. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  1986. if (unlikely(filp == ERR_PTR(-ECHILD)))
  1987. filp = path_openat(dfd, pathname, &nd, op, flags);
  1988. if (unlikely(filp == ERR_PTR(-ESTALE)))
  1989. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  1990. return filp;
  1991. }
  1992. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  1993. const char *name, const struct open_flags *op, int flags)
  1994. {
  1995. struct nameidata nd;
  1996. struct file *file;
  1997. nd.root.mnt = mnt;
  1998. nd.root.dentry = dentry;
  1999. flags |= LOOKUP_ROOT;
  2000. if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
  2001. return ERR_PTR(-ELOOP);
  2002. file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  2003. if (unlikely(file == ERR_PTR(-ECHILD)))
  2004. file = path_openat(-1, name, &nd, op, flags);
  2005. if (unlikely(file == ERR_PTR(-ESTALE)))
  2006. file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  2007. return file;
  2008. }
  2009. struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
  2010. {
  2011. struct dentry *dentry = ERR_PTR(-EEXIST);
  2012. struct nameidata nd;
  2013. int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
  2014. if (error)
  2015. return ERR_PTR(error);
  2016. /*
  2017. * Yucky last component or no last component at all?
  2018. * (foo/., foo/.., /////)
  2019. */
  2020. if (nd.last_type != LAST_NORM)
  2021. goto out;
  2022. nd.flags &= ~LOOKUP_PARENT;
  2023. nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2024. nd.intent.open.flags = O_EXCL;
  2025. /*
  2026. * Do the final lookup.
  2027. */
  2028. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2029. dentry = lookup_hash(&nd);
  2030. if (IS_ERR(dentry))
  2031. goto fail;
  2032. if (dentry->d_inode)
  2033. goto eexist;
  2034. /*
  2035. * Special case - lookup gave negative, but... we had foo/bar/
  2036. * From the vfs_mknod() POV we just have a negative dentry -
  2037. * all is fine. Let's be bastards - you had / on the end, you've
  2038. * been asking for (non-existent) directory. -ENOENT for you.
  2039. */
  2040. if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
  2041. dput(dentry);
  2042. dentry = ERR_PTR(-ENOENT);
  2043. goto fail;
  2044. }
  2045. *path = nd.path;
  2046. return dentry;
  2047. eexist:
  2048. dput(dentry);
  2049. dentry = ERR_PTR(-EEXIST);
  2050. fail:
  2051. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2052. out:
  2053. path_put(&nd.path);
  2054. return dentry;
  2055. }
  2056. EXPORT_SYMBOL(kern_path_create);
  2057. struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
  2058. {
  2059. char *tmp = getname(pathname);
  2060. struct dentry *res;
  2061. if (IS_ERR(tmp))
  2062. return ERR_CAST(tmp);
  2063. res = kern_path_create(dfd, tmp, path, is_dir);
  2064. putname(tmp);
  2065. return res;
  2066. }
  2067. EXPORT_SYMBOL(user_path_create);
  2068. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  2069. {
  2070. int error = may_create(dir, dentry);
  2071. if (error)
  2072. return error;
  2073. if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
  2074. !ns_capable(inode_userns(dir), CAP_MKNOD))
  2075. return -EPERM;
  2076. if (!dir->i_op->mknod)
  2077. return -EPERM;
  2078. error = devcgroup_inode_mknod(mode, dev);
  2079. if (error)
  2080. return error;
  2081. error = security_inode_mknod(dir, dentry, mode, dev);
  2082. if (error)
  2083. return error;
  2084. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2085. if (!error)
  2086. fsnotify_create(dir, dentry);
  2087. return error;
  2088. }
  2089. static int may_mknod(mode_t mode)
  2090. {
  2091. switch (mode & S_IFMT) {
  2092. case S_IFREG:
  2093. case S_IFCHR:
  2094. case S_IFBLK:
  2095. case S_IFIFO:
  2096. case S_IFSOCK:
  2097. case 0: /* zero mode translates to S_IFREG */
  2098. return 0;
  2099. case S_IFDIR:
  2100. return -EPERM;
  2101. default:
  2102. return -EINVAL;
  2103. }
  2104. }
  2105. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
  2106. unsigned, dev)
  2107. {
  2108. struct dentry *dentry;
  2109. struct path path;
  2110. int error;
  2111. if (S_ISDIR(mode))
  2112. return -EPERM;
  2113. dentry = user_path_create(dfd, filename, &path, 0);
  2114. if (IS_ERR(dentry))
  2115. return PTR_ERR(dentry);
  2116. if (!IS_POSIXACL(path.dentry->d_inode))
  2117. mode &= ~current_umask();
  2118. error = may_mknod(mode);
  2119. if (error)
  2120. goto out_dput;
  2121. error = mnt_want_write(path.mnt);
  2122. if (error)
  2123. goto out_dput;
  2124. error = security_path_mknod(&path, dentry, mode, dev);
  2125. if (error)
  2126. goto out_drop_write;
  2127. switch (mode & S_IFMT) {
  2128. case 0: case S_IFREG:
  2129. error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
  2130. break;
  2131. case S_IFCHR: case S_IFBLK:
  2132. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  2133. new_decode_dev(dev));
  2134. break;
  2135. case S_IFIFO: case S_IFSOCK:
  2136. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  2137. break;
  2138. }
  2139. out_drop_write:
  2140. mnt_drop_write(path.mnt);
  2141. out_dput:
  2142. dput(dentry);
  2143. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2144. path_put(&path);
  2145. return error;
  2146. }
  2147. SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
  2148. {
  2149. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2150. }
  2151. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2152. {
  2153. int error = may_create(dir, dentry);
  2154. if (error)
  2155. return error;
  2156. if (!dir->i_op->mkdir)
  2157. return -EPERM;
  2158. mode &= (S_IRWXUGO|S_ISVTX);
  2159. error = security_inode_mkdir(dir, dentry, mode);
  2160. if (error)
  2161. return error;
  2162. error = dir->i_op->mkdir(dir, dentry, mode);
  2163. if (!error)
  2164. fsnotify_mkdir(dir, dentry);
  2165. return error;
  2166. }
  2167. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
  2168. {
  2169. struct dentry *dentry;
  2170. struct path path;
  2171. int error;
  2172. dentry = user_path_create(dfd, pathname, &path, 1);
  2173. if (IS_ERR(dentry))
  2174. return PTR_ERR(dentry);
  2175. if (!IS_POSIXACL(path.dentry->d_inode))
  2176. mode &= ~current_umask();
  2177. error = mnt_want_write(path.mnt);
  2178. if (error)
  2179. goto out_dput;
  2180. error = security_path_mkdir(&path, dentry, mode);
  2181. if (error)
  2182. goto out_drop_write;
  2183. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  2184. out_drop_write:
  2185. mnt_drop_write(path.mnt);
  2186. out_dput:
  2187. dput(dentry);
  2188. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2189. path_put(&path);
  2190. return error;
  2191. }
  2192. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
  2193. {
  2194. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2195. }
  2196. /*
  2197. * The dentry_unhash() helper will try to drop the dentry early: we
  2198. * should have a usage count of 2 if we're the only user of this
  2199. * dentry, and if that is true (possibly after pruning the dcache),
  2200. * then we drop the dentry now.
  2201. *
  2202. * A low-level filesystem can, if it choses, legally
  2203. * do a
  2204. *
  2205. * if (!d_unhashed(dentry))
  2206. * return -EBUSY;
  2207. *
  2208. * if it cannot handle the case of removing a directory
  2209. * that is still in use by something else..
  2210. */
  2211. void dentry_unhash(struct dentry *dentry)
  2212. {
  2213. shrink_dcache_parent(dentry);
  2214. spin_lock(&dentry->d_lock);
  2215. if (dentry->d_count == 1)
  2216. __d_drop(dentry);
  2217. spin_unlock(&dentry->d_lock);
  2218. }
  2219. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2220. {
  2221. int error = may_delete(dir, dentry, 1);
  2222. if (error)
  2223. return error;
  2224. if (!dir->i_op->rmdir)
  2225. return -EPERM;
  2226. mutex_lock(&dentry->d_inode->i_mutex);
  2227. error = -EBUSY;
  2228. if (d_mountpoint(dentry))
  2229. goto out;
  2230. error = security_inode_rmdir(dir, dentry);
  2231. if (error)
  2232. goto out;
  2233. shrink_dcache_parent(dentry);
  2234. error = dir->i_op->rmdir(dir, dentry);
  2235. if (error)
  2236. goto out;
  2237. dentry->d_inode->i_flags |= S_DEAD;
  2238. dont_mount(dentry);
  2239. out:
  2240. mutex_unlock(&dentry->d_inode->i_mutex);
  2241. if (!error)
  2242. d_delete(dentry);
  2243. return error;
  2244. }
  2245. static long do_rmdir(int dfd, const char __user *pathname)
  2246. {
  2247. int error = 0;
  2248. char * name;
  2249. struct dentry *dentry;
  2250. struct nameidata nd;
  2251. error = user_path_parent(dfd, pathname, &nd, &name);
  2252. if (error)
  2253. return error;
  2254. switch(nd.last_type) {
  2255. case LAST_DOTDOT:
  2256. error = -ENOTEMPTY;
  2257. goto exit1;
  2258. case LAST_DOT:
  2259. error = -EINVAL;
  2260. goto exit1;
  2261. case LAST_ROOT:
  2262. error = -EBUSY;
  2263. goto exit1;
  2264. }
  2265. nd.flags &= ~LOOKUP_PARENT;
  2266. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2267. dentry = lookup_hash(&nd);
  2268. error = PTR_ERR(dentry);
  2269. if (IS_ERR(dentry))
  2270. goto exit2;
  2271. if (!dentry->d_inode) {
  2272. error = -ENOENT;
  2273. goto exit3;
  2274. }
  2275. error = mnt_want_write(nd.path.mnt);
  2276. if (error)
  2277. goto exit3;
  2278. error = security_path_rmdir(&nd.path, dentry);
  2279. if (error)
  2280. goto exit4;
  2281. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2282. exit4:
  2283. mnt_drop_write(nd.path.mnt);
  2284. exit3:
  2285. dput(dentry);
  2286. exit2:
  2287. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2288. exit1:
  2289. path_put(&nd.path);
  2290. putname(name);
  2291. return error;
  2292. }
  2293. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2294. {
  2295. return do_rmdir(AT_FDCWD, pathname);
  2296. }
  2297. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2298. {
  2299. int error = may_delete(dir, dentry, 0);
  2300. if (error)
  2301. return error;
  2302. if (!dir->i_op->unlink)
  2303. return -EPERM;
  2304. mutex_lock(&dentry->d_inode->i_mutex);
  2305. if (d_mountpoint(dentry))
  2306. error = -EBUSY;
  2307. else {
  2308. error = security_inode_unlink(dir, dentry);
  2309. if (!error) {
  2310. error = dir->i_op->unlink(dir, dentry);
  2311. if (!error)
  2312. dont_mount(dentry);
  2313. }
  2314. }
  2315. mutex_unlock(&dentry->d_inode->i_mutex);
  2316. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2317. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2318. fsnotify_link_count(dentry->d_inode);
  2319. d_delete(dentry);
  2320. }
  2321. return error;
  2322. }
  2323. /*
  2324. * Make sure that the actual truncation of the file will occur outside its
  2325. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2326. * writeout happening, and we don't want to prevent access to the directory
  2327. * while waiting on the I/O.
  2328. */
  2329. static long do_unlinkat(int dfd, const char __user *pathname)
  2330. {
  2331. int error;
  2332. char *name;
  2333. struct dentry *dentry;
  2334. struct nameidata nd;
  2335. struct inode *inode = NULL;
  2336. error = user_path_parent(dfd, pathname, &nd, &name);
  2337. if (error)
  2338. return error;
  2339. error = -EISDIR;
  2340. if (nd.last_type != LAST_NORM)
  2341. goto exit1;
  2342. nd.flags &= ~LOOKUP_PARENT;
  2343. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2344. dentry = lookup_hash(&nd);
  2345. error = PTR_ERR(dentry);
  2346. if (!IS_ERR(dentry)) {
  2347. /* Why not before? Because we want correct error value */
  2348. if (nd.last.name[nd.last.len])
  2349. goto slashes;
  2350. inode = dentry->d_inode;
  2351. if (!inode)
  2352. goto slashes;
  2353. ihold(inode);
  2354. error = mnt_want_write(nd.path.mnt);
  2355. if (error)
  2356. goto exit2;
  2357. error = security_path_unlink(&nd.path, dentry);
  2358. if (error)
  2359. goto exit3;
  2360. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2361. exit3:
  2362. mnt_drop_write(nd.path.mnt);
  2363. exit2:
  2364. dput(dentry);
  2365. }
  2366. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2367. if (inode)
  2368. iput(inode); /* truncate the inode here */
  2369. exit1:
  2370. path_put(&nd.path);
  2371. putname(name);
  2372. return error;
  2373. slashes:
  2374. error = !dentry->d_inode ? -ENOENT :
  2375. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2376. goto exit2;
  2377. }
  2378. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2379. {
  2380. if ((flag & ~AT_REMOVEDIR) != 0)
  2381. return -EINVAL;
  2382. if (flag & AT_REMOVEDIR)
  2383. return do_rmdir(dfd, pathname);
  2384. return do_unlinkat(dfd, pathname);
  2385. }
  2386. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2387. {
  2388. return do_unlinkat(AT_FDCWD, pathname);
  2389. }
  2390. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2391. {
  2392. int error = may_create(dir, dentry);
  2393. if (error)
  2394. return error;
  2395. if (!dir->i_op->symlink)
  2396. return -EPERM;
  2397. error = security_inode_symlink(dir, dentry, oldname);
  2398. if (error)
  2399. return error;
  2400. error = dir->i_op->symlink(dir, dentry, oldname);
  2401. if (!error)
  2402. fsnotify_create(dir, dentry);
  2403. return error;
  2404. }
  2405. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2406. int, newdfd, const char __user *, newname)
  2407. {
  2408. int error;
  2409. char *from;
  2410. struct dentry *dentry;
  2411. struct path path;
  2412. from = getname(oldname);
  2413. if (IS_ERR(from))
  2414. return PTR_ERR(from);
  2415. dentry = user_path_create(newdfd, newname, &path, 0);
  2416. error = PTR_ERR(dentry);
  2417. if (IS_ERR(dentry))
  2418. goto out_putname;
  2419. error = mnt_want_write(path.mnt);
  2420. if (error)
  2421. goto out_dput;
  2422. error = security_path_symlink(&path, dentry, from);
  2423. if (error)
  2424. goto out_drop_write;
  2425. error = vfs_symlink(path.dentry->d_inode, dentry, from);
  2426. out_drop_write:
  2427. mnt_drop_write(path.mnt);
  2428. out_dput:
  2429. dput(dentry);
  2430. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2431. path_put(&path);
  2432. out_putname:
  2433. putname(from);
  2434. return error;
  2435. }
  2436. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2437. {
  2438. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2439. }
  2440. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2441. {
  2442. struct inode *inode = old_dentry->d_inode;
  2443. int error;
  2444. if (!inode)
  2445. return -ENOENT;
  2446. error = may_create(dir, new_dentry);
  2447. if (error)
  2448. return error;
  2449. if (dir->i_sb != inode->i_sb)
  2450. return -EXDEV;
  2451. /*
  2452. * A link to an append-only or immutable file cannot be created.
  2453. */
  2454. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2455. return -EPERM;
  2456. if (!dir->i_op->link)
  2457. return -EPERM;
  2458. if (S_ISDIR(inode->i_mode))
  2459. return -EPERM;
  2460. error = security_inode_link(old_dentry, dir, new_dentry);
  2461. if (error)
  2462. return error;
  2463. mutex_lock(&inode->i_mutex);
  2464. /* Make sure we don't allow creating hardlink to an unlinked file */
  2465. if (inode->i_nlink == 0)
  2466. error = -ENOENT;
  2467. else
  2468. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2469. mutex_unlock(&inode->i_mutex);
  2470. if (!error)
  2471. fsnotify_link(dir, inode, new_dentry);
  2472. return error;
  2473. }
  2474. /*
  2475. * Hardlinks are often used in delicate situations. We avoid
  2476. * security-related surprises by not following symlinks on the
  2477. * newname. --KAB
  2478. *
  2479. * We don't follow them on the oldname either to be compatible
  2480. * with linux 2.0, and to avoid hard-linking to directories
  2481. * and other special files. --ADM
  2482. */
  2483. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2484. int, newdfd, const char __user *, newname, int, flags)
  2485. {
  2486. struct dentry *new_dentry;
  2487. struct path old_path, new_path;
  2488. int how = 0;
  2489. int error;
  2490. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  2491. return -EINVAL;
  2492. /*
  2493. * To use null names we require CAP_DAC_READ_SEARCH
  2494. * This ensures that not everyone will be able to create
  2495. * handlink using the passed filedescriptor.
  2496. */
  2497. if (flags & AT_EMPTY_PATH) {
  2498. if (!capable(CAP_DAC_READ_SEARCH))
  2499. return -ENOENT;
  2500. how = LOOKUP_EMPTY;
  2501. }
  2502. if (flags & AT_SYMLINK_FOLLOW)
  2503. how |= LOOKUP_FOLLOW;
  2504. error = user_path_at(olddfd, oldname, how, &old_path);
  2505. if (error)
  2506. return error;
  2507. new_dentry = user_path_create(newdfd, newname, &new_path, 0);
  2508. error = PTR_ERR(new_dentry);
  2509. if (IS_ERR(new_dentry))
  2510. goto out;
  2511. error = -EXDEV;
  2512. if (old_path.mnt != new_path.mnt)
  2513. goto out_dput;
  2514. error = mnt_want_write(new_path.mnt);
  2515. if (error)
  2516. goto out_dput;
  2517. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  2518. if (error)
  2519. goto out_drop_write;
  2520. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
  2521. out_drop_write:
  2522. mnt_drop_write(new_path.mnt);
  2523. out_dput:
  2524. dput(new_dentry);
  2525. mutex_unlock(&new_path.dentry->d_inode->i_mutex);
  2526. path_put(&new_path);
  2527. out:
  2528. path_put(&old_path);
  2529. return error;
  2530. }
  2531. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2532. {
  2533. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2534. }
  2535. /*
  2536. * The worst of all namespace operations - renaming directory. "Perverted"
  2537. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2538. * Problems:
  2539. * a) we can get into loop creation. Check is done in is_subdir().
  2540. * b) race potential - two innocent renames can create a loop together.
  2541. * That's where 4.4 screws up. Current fix: serialization on
  2542. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2543. * story.
  2544. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2545. * And that - after we got ->i_mutex on parents (until then we don't know
  2546. * whether the target exists). Solution: try to be smart with locking
  2547. * order for inodes. We rely on the fact that tree topology may change
  2548. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2549. * move will be locked. Thus we can rank directories by the tree
  2550. * (ancestors first) and rank all non-directories after them.
  2551. * That works since everybody except rename does "lock parent, lookup,
  2552. * lock child" and rename is under ->s_vfs_rename_mutex.
  2553. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2554. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2555. * we'd better make sure that there's no link(2) for them.
  2556. * d) conversion from fhandle to dentry may come in the wrong moment - when
  2557. * we are removing the target. Solution: we will have to grab ->i_mutex
  2558. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2559. * ->i_mutex on parents, which works but leads to some truly excessive
  2560. * locking].
  2561. */
  2562. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2563. struct inode *new_dir, struct dentry *new_dentry)
  2564. {
  2565. int error = 0;
  2566. struct inode *target = new_dentry->d_inode;
  2567. /*
  2568. * If we are going to change the parent - check write permissions,
  2569. * we'll need to flip '..'.
  2570. */
  2571. if (new_dir != old_dir) {
  2572. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2573. if (error)
  2574. return error;
  2575. }
  2576. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2577. if (error)
  2578. return error;
  2579. if (target)
  2580. mutex_lock(&target->i_mutex);
  2581. error = -EBUSY;
  2582. if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  2583. goto out;
  2584. if (target)
  2585. shrink_dcache_parent(new_dentry);
  2586. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2587. if (error)
  2588. goto out;
  2589. if (target) {
  2590. target->i_flags |= S_DEAD;
  2591. dont_mount(new_dentry);
  2592. }
  2593. out:
  2594. if (target)
  2595. mutex_unlock(&target->i_mutex);
  2596. if (!error)
  2597. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2598. d_move(old_dentry,new_dentry);
  2599. return error;
  2600. }
  2601. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2602. struct inode *new_dir, struct dentry *new_dentry)
  2603. {
  2604. struct inode *target = new_dentry->d_inode;
  2605. int error;
  2606. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2607. if (error)
  2608. return error;
  2609. dget(new_dentry);
  2610. if (target)
  2611. mutex_lock(&target->i_mutex);
  2612. error = -EBUSY;
  2613. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2614. goto out;
  2615. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2616. if (error)
  2617. goto out;
  2618. if (target)
  2619. dont_mount(new_dentry);
  2620. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2621. d_move(old_dentry, new_dentry);
  2622. out:
  2623. if (target)
  2624. mutex_unlock(&target->i_mutex);
  2625. dput(new_dentry);
  2626. return error;
  2627. }
  2628. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2629. struct inode *new_dir, struct dentry *new_dentry)
  2630. {
  2631. int error;
  2632. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2633. const unsigned char *old_name;
  2634. if (old_dentry->d_inode == new_dentry->d_inode)
  2635. return 0;
  2636. error = may_delete(old_dir, old_dentry, is_dir);
  2637. if (error)
  2638. return error;
  2639. if (!new_dentry->d_inode)
  2640. error = may_create(new_dir, new_dentry);
  2641. else
  2642. error = may_delete(new_dir, new_dentry, is_dir);
  2643. if (error)
  2644. return error;
  2645. if (!old_dir->i_op->rename)
  2646. return -EPERM;
  2647. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2648. if (is_dir)
  2649. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2650. else
  2651. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2652. if (!error)
  2653. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2654. new_dentry->d_inode, old_dentry);
  2655. fsnotify_oldname_free(old_name);
  2656. return error;
  2657. }
  2658. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2659. int, newdfd, const char __user *, newname)
  2660. {
  2661. struct dentry *old_dir, *new_dir;
  2662. struct dentry *old_dentry, *new_dentry;
  2663. struct dentry *trap;
  2664. struct nameidata oldnd, newnd;
  2665. char *from;
  2666. char *to;
  2667. int error;
  2668. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2669. if (error)
  2670. goto exit;
  2671. error = user_path_parent(newdfd, newname, &newnd, &to);
  2672. if (error)
  2673. goto exit1;
  2674. error = -EXDEV;
  2675. if (oldnd.path.mnt != newnd.path.mnt)
  2676. goto exit2;
  2677. old_dir = oldnd.path.dentry;
  2678. error = -EBUSY;
  2679. if (oldnd.last_type != LAST_NORM)
  2680. goto exit2;
  2681. new_dir = newnd.path.dentry;
  2682. if (newnd.last_type != LAST_NORM)
  2683. goto exit2;
  2684. oldnd.flags &= ~LOOKUP_PARENT;
  2685. newnd.flags &= ~LOOKUP_PARENT;
  2686. newnd.flags |= LOOKUP_RENAME_TARGET;
  2687. trap = lock_rename(new_dir, old_dir);
  2688. old_dentry = lookup_hash(&oldnd);
  2689. error = PTR_ERR(old_dentry);
  2690. if (IS_ERR(old_dentry))
  2691. goto exit3;
  2692. /* source must exist */
  2693. error = -ENOENT;
  2694. if (!old_dentry->d_inode)
  2695. goto exit4;
  2696. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2697. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2698. error = -ENOTDIR;
  2699. if (oldnd.last.name[oldnd.last.len])
  2700. goto exit4;
  2701. if (newnd.last.name[newnd.last.len])
  2702. goto exit4;
  2703. }
  2704. /* source should not be ancestor of target */
  2705. error = -EINVAL;
  2706. if (old_dentry == trap)
  2707. goto exit4;
  2708. new_dentry = lookup_hash(&newnd);
  2709. error = PTR_ERR(new_dentry);
  2710. if (IS_ERR(new_dentry))
  2711. goto exit4;
  2712. /* target should not be an ancestor of source */
  2713. error = -ENOTEMPTY;
  2714. if (new_dentry == trap)
  2715. goto exit5;
  2716. error = mnt_want_write(oldnd.path.mnt);
  2717. if (error)
  2718. goto exit5;
  2719. error = security_path_rename(&oldnd.path, old_dentry,
  2720. &newnd.path, new_dentry);
  2721. if (error)
  2722. goto exit6;
  2723. error = vfs_rename(old_dir->d_inode, old_dentry,
  2724. new_dir->d_inode, new_dentry);
  2725. exit6:
  2726. mnt_drop_write(oldnd.path.mnt);
  2727. exit5:
  2728. dput(new_dentry);
  2729. exit4:
  2730. dput(old_dentry);
  2731. exit3:
  2732. unlock_rename(new_dir, old_dir);
  2733. exit2:
  2734. path_put(&newnd.path);
  2735. putname(to);
  2736. exit1:
  2737. path_put(&oldnd.path);
  2738. putname(from);
  2739. exit:
  2740. return error;
  2741. }
  2742. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2743. {
  2744. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2745. }
  2746. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2747. {
  2748. int len;
  2749. len = PTR_ERR(link);
  2750. if (IS_ERR(link))
  2751. goto out;
  2752. len = strlen(link);
  2753. if (len > (unsigned) buflen)
  2754. len = buflen;
  2755. if (copy_to_user(buffer, link, len))
  2756. len = -EFAULT;
  2757. out:
  2758. return len;
  2759. }
  2760. /*
  2761. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2762. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2763. * using) it for any given inode is up to filesystem.
  2764. */
  2765. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2766. {
  2767. struct nameidata nd;
  2768. void *cookie;
  2769. int res;
  2770. nd.depth = 0;
  2771. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2772. if (IS_ERR(cookie))
  2773. return PTR_ERR(cookie);
  2774. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2775. if (dentry->d_inode->i_op->put_link)
  2776. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2777. return res;
  2778. }
  2779. int vfs_follow_link(struct nameidata *nd, const char *link)
  2780. {
  2781. return __vfs_follow_link(nd, link);
  2782. }
  2783. /* get the link contents into pagecache */
  2784. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2785. {
  2786. char *kaddr;
  2787. struct page *page;
  2788. struct address_space *mapping = dentry->d_inode->i_mapping;
  2789. page = read_mapping_page(mapping, 0, NULL);
  2790. if (IS_ERR(page))
  2791. return (char*)page;
  2792. *ppage = page;
  2793. kaddr = kmap(page);
  2794. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  2795. return kaddr;
  2796. }
  2797. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2798. {
  2799. struct page *page = NULL;
  2800. char *s = page_getlink(dentry, &page);
  2801. int res = vfs_readlink(dentry,buffer,buflen,s);
  2802. if (page) {
  2803. kunmap(page);
  2804. page_cache_release(page);
  2805. }
  2806. return res;
  2807. }
  2808. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2809. {
  2810. struct page *page = NULL;
  2811. nd_set_link(nd, page_getlink(dentry, &page));
  2812. return page;
  2813. }
  2814. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2815. {
  2816. struct page *page = cookie;
  2817. if (page) {
  2818. kunmap(page);
  2819. page_cache_release(page);
  2820. }
  2821. }
  2822. /*
  2823. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  2824. */
  2825. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  2826. {
  2827. struct address_space *mapping = inode->i_mapping;
  2828. struct page *page;
  2829. void *fsdata;
  2830. int err;
  2831. char *kaddr;
  2832. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  2833. if (nofs)
  2834. flags |= AOP_FLAG_NOFS;
  2835. retry:
  2836. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2837. flags, &page, &fsdata);
  2838. if (err)
  2839. goto fail;
  2840. kaddr = kmap_atomic(page, KM_USER0);
  2841. memcpy(kaddr, symname, len-1);
  2842. kunmap_atomic(kaddr, KM_USER0);
  2843. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2844. page, fsdata);
  2845. if (err < 0)
  2846. goto fail;
  2847. if (err < len-1)
  2848. goto retry;
  2849. mark_inode_dirty(inode);
  2850. return 0;
  2851. fail:
  2852. return err;
  2853. }
  2854. int page_symlink(struct inode *inode, const char *symname, int len)
  2855. {
  2856. return __page_symlink(inode, symname, len,
  2857. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  2858. }
  2859. const struct inode_operations page_symlink_inode_operations = {
  2860. .readlink = generic_readlink,
  2861. .follow_link = page_follow_link_light,
  2862. .put_link = page_put_link,
  2863. };
  2864. EXPORT_SYMBOL(user_path_at);
  2865. EXPORT_SYMBOL(follow_down_one);
  2866. EXPORT_SYMBOL(follow_down);
  2867. EXPORT_SYMBOL(follow_up);
  2868. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2869. EXPORT_SYMBOL(getname);
  2870. EXPORT_SYMBOL(lock_rename);
  2871. EXPORT_SYMBOL(lookup_one_len);
  2872. EXPORT_SYMBOL(page_follow_link_light);
  2873. EXPORT_SYMBOL(page_put_link);
  2874. EXPORT_SYMBOL(page_readlink);
  2875. EXPORT_SYMBOL(__page_symlink);
  2876. EXPORT_SYMBOL(page_symlink);
  2877. EXPORT_SYMBOL(page_symlink_inode_operations);
  2878. EXPORT_SYMBOL(kern_path);
  2879. EXPORT_SYMBOL(vfs_path_lookup);
  2880. EXPORT_SYMBOL(inode_permission);
  2881. EXPORT_SYMBOL(unlock_rename);
  2882. EXPORT_SYMBOL(vfs_create);
  2883. EXPORT_SYMBOL(vfs_follow_link);
  2884. EXPORT_SYMBOL(vfs_link);
  2885. EXPORT_SYMBOL(vfs_mkdir);
  2886. EXPORT_SYMBOL(vfs_mknod);
  2887. EXPORT_SYMBOL(generic_permission);
  2888. EXPORT_SYMBOL(vfs_readlink);
  2889. EXPORT_SYMBOL(vfs_rename);
  2890. EXPORT_SYMBOL(vfs_rmdir);
  2891. EXPORT_SYMBOL(vfs_symlink);
  2892. EXPORT_SYMBOL(vfs_unlink);
  2893. EXPORT_SYMBOL(dentry_unhash);
  2894. EXPORT_SYMBOL(generic_readlink);