namei.c 111 KB

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