namei.c 86 KB

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