namei.c 91 KB

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