namei.c 85 KB

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