namei.c 80 KB

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