namei.c 85 KB

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