namei.c 89 KB

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