namei.c 82 KB

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