namei.c 82 KB

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