namei.c 91 KB

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