namei.c 83 KB

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