namei.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437
  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. * See if the low-level filesystem might want
  1087. * to use its own hash..
  1088. */
  1089. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1090. err = parent->d_op->d_hash(parent, nd->inode, name);
  1091. if (err < 0)
  1092. return err;
  1093. }
  1094. /*
  1095. * Rename seqlock is not required here because in the off chance
  1096. * of a false negative due to a concurrent rename, we're going to
  1097. * do the non-racy lookup, below.
  1098. */
  1099. if (nd->flags & LOOKUP_RCU) {
  1100. unsigned seq;
  1101. *inode = nd->inode;
  1102. dentry = __d_lookup_rcu(parent, name, &seq, inode);
  1103. if (!dentry) {
  1104. if (nameidata_drop_rcu(nd))
  1105. return -ECHILD;
  1106. goto need_lookup;
  1107. }
  1108. /* Memory barrier in read_seqcount_begin of child is enough */
  1109. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1110. return -ECHILD;
  1111. nd->seq = seq;
  1112. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1113. dentry = do_revalidate_rcu(dentry, nd);
  1114. if (!dentry)
  1115. goto need_lookup;
  1116. if (IS_ERR(dentry))
  1117. goto fail;
  1118. if (!(nd->flags & LOOKUP_RCU))
  1119. goto done;
  1120. }
  1121. path->mnt = mnt;
  1122. path->dentry = dentry;
  1123. if (likely(__follow_mount_rcu(nd, path, inode, false)))
  1124. return 0;
  1125. if (nameidata_drop_rcu(nd))
  1126. return -ECHILD;
  1127. /* fallthru */
  1128. }
  1129. dentry = __d_lookup(parent, name);
  1130. if (!dentry)
  1131. goto need_lookup;
  1132. found:
  1133. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1134. dentry = do_revalidate(dentry, nd);
  1135. if (!dentry)
  1136. goto need_lookup;
  1137. if (IS_ERR(dentry))
  1138. goto fail;
  1139. }
  1140. done:
  1141. path->mnt = mnt;
  1142. path->dentry = dentry;
  1143. err = follow_managed(path, nd->flags);
  1144. if (unlikely(err < 0)) {
  1145. path_put_conditional(path, nd);
  1146. return err;
  1147. }
  1148. *inode = path->dentry->d_inode;
  1149. return 0;
  1150. need_lookup:
  1151. dir = parent->d_inode;
  1152. BUG_ON(nd->inode != dir);
  1153. mutex_lock(&dir->i_mutex);
  1154. /*
  1155. * First re-do the cached lookup just in case it was created
  1156. * while we waited for the directory semaphore, or the first
  1157. * lookup failed due to an unrelated rename.
  1158. *
  1159. * This could use version numbering or similar to avoid unnecessary
  1160. * cache lookups, but then we'd have to do the first lookup in the
  1161. * non-racy way. However in the common case here, everything should
  1162. * be hot in cache, so would it be a big win?
  1163. */
  1164. dentry = d_lookup(parent, name);
  1165. if (likely(!dentry)) {
  1166. dentry = d_alloc_and_lookup(parent, name, nd);
  1167. mutex_unlock(&dir->i_mutex);
  1168. if (IS_ERR(dentry))
  1169. goto fail;
  1170. goto done;
  1171. }
  1172. /*
  1173. * Uhhuh! Nasty case: the cache was re-populated while
  1174. * we waited on the semaphore. Need to revalidate.
  1175. */
  1176. mutex_unlock(&dir->i_mutex);
  1177. goto found;
  1178. fail:
  1179. return PTR_ERR(dentry);
  1180. }
  1181. static inline int may_lookup(struct nameidata *nd)
  1182. {
  1183. if (nd->flags & LOOKUP_RCU) {
  1184. int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
  1185. if (err != -ECHILD)
  1186. return err;
  1187. if (nameidata_drop_rcu(nd))
  1188. return -ECHILD;
  1189. }
  1190. return exec_permission(nd->inode, 0);
  1191. }
  1192. static inline int handle_dots(struct nameidata *nd, int type)
  1193. {
  1194. if (type == LAST_DOTDOT) {
  1195. if (nd->flags & LOOKUP_RCU) {
  1196. if (follow_dotdot_rcu(nd))
  1197. return -ECHILD;
  1198. } else
  1199. follow_dotdot(nd);
  1200. }
  1201. return 0;
  1202. }
  1203. static void terminate_walk(struct nameidata *nd)
  1204. {
  1205. if (!(nd->flags & LOOKUP_RCU)) {
  1206. path_put(&nd->path);
  1207. } else {
  1208. nd->flags &= ~LOOKUP_RCU;
  1209. nd->root.mnt = NULL;
  1210. rcu_read_unlock();
  1211. br_read_unlock(vfsmount_lock);
  1212. }
  1213. }
  1214. /*
  1215. * Name resolution.
  1216. * This is the basic name resolution function, turning a pathname into
  1217. * the final dentry. We expect 'base' to be positive and a directory.
  1218. *
  1219. * Returns 0 and nd will have valid dentry and mnt on success.
  1220. * Returns error and drops reference to input namei data on failure.
  1221. */
  1222. static int link_path_walk(const char *name, struct nameidata *nd)
  1223. {
  1224. struct path next;
  1225. int err;
  1226. unsigned int lookup_flags = nd->flags;
  1227. while (*name=='/')
  1228. name++;
  1229. if (!*name)
  1230. return 0;
  1231. if (nd->depth)
  1232. lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
  1233. /* At this point we know we have a real path component. */
  1234. for(;;) {
  1235. struct inode *inode;
  1236. unsigned long hash;
  1237. struct qstr this;
  1238. unsigned int c;
  1239. int type;
  1240. nd->flags |= LOOKUP_CONTINUE;
  1241. err = may_lookup(nd);
  1242. if (err)
  1243. break;
  1244. this.name = name;
  1245. c = *(const unsigned char *)name;
  1246. hash = init_name_hash();
  1247. do {
  1248. name++;
  1249. hash = partial_name_hash(c, hash);
  1250. c = *(const unsigned char *)name;
  1251. } while (c && (c != '/'));
  1252. this.len = name - (const char *) this.name;
  1253. this.hash = end_name_hash(hash);
  1254. type = LAST_NORM;
  1255. if (this.name[0] == '.') switch (this.len) {
  1256. case 2:
  1257. if (this.name[1] == '.') {
  1258. type = LAST_DOTDOT;
  1259. nd->flags |= LOOKUP_JUMPED;
  1260. }
  1261. break;
  1262. case 1:
  1263. type = LAST_DOT;
  1264. }
  1265. if (likely(type == LAST_NORM))
  1266. nd->flags &= ~LOOKUP_JUMPED;
  1267. /* remove trailing slashes? */
  1268. if (!c)
  1269. goto last_component;
  1270. while (*++name == '/');
  1271. if (!*name)
  1272. goto last_with_slashes;
  1273. /*
  1274. * "." and ".." are special - ".." especially so because it has
  1275. * to be able to know about the current root directory and
  1276. * parent relationships.
  1277. */
  1278. if (unlikely(type != LAST_NORM)) {
  1279. if (handle_dots(nd, type))
  1280. return -ECHILD;
  1281. continue;
  1282. }
  1283. /* This does the actual lookups.. */
  1284. err = do_lookup(nd, &this, &next, &inode);
  1285. if (err)
  1286. break;
  1287. if (inode && inode->i_op->follow_link) {
  1288. err = do_follow_link(inode, &next, nd);
  1289. if (err)
  1290. return err;
  1291. nd->inode = nd->path.dentry->d_inode;
  1292. } else {
  1293. path_to_nameidata(&next, nd);
  1294. nd->inode = inode;
  1295. }
  1296. err = -ENOENT;
  1297. if (!nd->inode)
  1298. break;
  1299. err = -ENOTDIR;
  1300. if (!nd->inode->i_op->lookup)
  1301. break;
  1302. continue;
  1303. /* here ends the main loop */
  1304. last_with_slashes:
  1305. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1306. last_component:
  1307. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  1308. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  1309. if (lookup_flags & LOOKUP_PARENT)
  1310. goto lookup_parent;
  1311. if (unlikely(type != LAST_NORM))
  1312. return handle_dots(nd, type);
  1313. err = do_lookup(nd, &this, &next, &inode);
  1314. if (err)
  1315. break;
  1316. if (inode && unlikely(inode->i_op->follow_link) &&
  1317. (lookup_flags & LOOKUP_FOLLOW)) {
  1318. err = do_follow_link(inode, &next, nd);
  1319. if (err)
  1320. return err;
  1321. nd->inode = nd->path.dentry->d_inode;
  1322. } else {
  1323. path_to_nameidata(&next, nd);
  1324. nd->inode = inode;
  1325. }
  1326. err = -ENOENT;
  1327. if (!nd->inode)
  1328. break;
  1329. if (lookup_flags & LOOKUP_DIRECTORY) {
  1330. err = -ENOTDIR;
  1331. if (!nd->inode->i_op->lookup)
  1332. break;
  1333. }
  1334. return 0;
  1335. lookup_parent:
  1336. nd->last = this;
  1337. nd->last_type = type;
  1338. return 0;
  1339. }
  1340. terminate_walk(nd);
  1341. return err;
  1342. }
  1343. static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
  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. nd->file = NULL;
  1353. if (*name=='/') {
  1354. if (flags & LOOKUP_RCU) {
  1355. br_read_lock(vfsmount_lock);
  1356. rcu_read_lock();
  1357. set_root_rcu(nd);
  1358. } else {
  1359. set_root(nd);
  1360. path_get(&nd->root);
  1361. }
  1362. nd->path = nd->root;
  1363. } else if (dfd == AT_FDCWD) {
  1364. if (flags & LOOKUP_RCU) {
  1365. struct fs_struct *fs = current->fs;
  1366. unsigned seq;
  1367. br_read_lock(vfsmount_lock);
  1368. rcu_read_lock();
  1369. do {
  1370. seq = read_seqcount_begin(&fs->seq);
  1371. nd->path = fs->pwd;
  1372. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1373. } while (read_seqcount_retry(&fs->seq, seq));
  1374. } else {
  1375. get_fs_pwd(current->fs, &nd->path);
  1376. }
  1377. } else {
  1378. struct dentry *dentry;
  1379. file = fget_light(dfd, &fput_needed);
  1380. retval = -EBADF;
  1381. if (!file)
  1382. goto out_fail;
  1383. dentry = file->f_path.dentry;
  1384. retval = -ENOTDIR;
  1385. if (!S_ISDIR(dentry->d_inode->i_mode))
  1386. goto fput_fail;
  1387. retval = file_permission(file, MAY_EXEC);
  1388. if (retval)
  1389. goto fput_fail;
  1390. nd->path = file->f_path;
  1391. if (flags & LOOKUP_RCU) {
  1392. if (fput_needed)
  1393. nd->file = file;
  1394. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1395. br_read_lock(vfsmount_lock);
  1396. rcu_read_lock();
  1397. } else {
  1398. path_get(&file->f_path);
  1399. fput_light(file, fput_needed);
  1400. }
  1401. }
  1402. nd->inode = nd->path.dentry->d_inode;
  1403. return 0;
  1404. fput_fail:
  1405. fput_light(file, fput_needed);
  1406. out_fail:
  1407. return retval;
  1408. }
  1409. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1410. static int path_lookupat(int dfd, const char *name,
  1411. unsigned int flags, struct nameidata *nd)
  1412. {
  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);
  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 (nd->file) {
  1442. fput(nd->file);
  1443. nd->file = NULL;
  1444. }
  1445. if (nd->root.mnt) {
  1446. path_put(&nd->root);
  1447. nd->root.mnt = NULL;
  1448. }
  1449. return retval;
  1450. }
  1451. static int do_path_lookup(int dfd, const char *name,
  1452. unsigned int flags, struct nameidata *nd)
  1453. {
  1454. int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  1455. if (unlikely(retval == -ECHILD))
  1456. retval = path_lookupat(dfd, name, flags, nd);
  1457. if (unlikely(retval == -ESTALE))
  1458. retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
  1459. if (likely(!retval)) {
  1460. if (unlikely(!audit_dummy_context())) {
  1461. if (nd->path.dentry && nd->inode)
  1462. audit_inode(name, nd->path.dentry);
  1463. }
  1464. }
  1465. return retval;
  1466. }
  1467. int kern_path_parent(const char *name, struct nameidata *nd)
  1468. {
  1469. return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
  1470. }
  1471. int kern_path(const char *name, unsigned int flags, struct path *path)
  1472. {
  1473. struct nameidata nd;
  1474. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1475. if (!res)
  1476. *path = nd.path;
  1477. return res;
  1478. }
  1479. /**
  1480. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1481. * @dentry: pointer to dentry of the base directory
  1482. * @mnt: pointer to vfs mount of the base directory
  1483. * @name: pointer to file name
  1484. * @flags: lookup flags
  1485. * @nd: pointer to nameidata
  1486. */
  1487. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1488. const char *name, unsigned int flags,
  1489. struct nameidata *nd)
  1490. {
  1491. int result;
  1492. /* same as do_path_lookup */
  1493. nd->last_type = LAST_ROOT;
  1494. nd->flags = flags | LOOKUP_JUMPED;
  1495. nd->depth = 0;
  1496. nd->path.dentry = dentry;
  1497. nd->path.mnt = mnt;
  1498. path_get(&nd->path);
  1499. nd->root = nd->path;
  1500. path_get(&nd->root);
  1501. nd->inode = nd->path.dentry->d_inode;
  1502. current->total_link_count = 0;
  1503. result = link_path_walk(name, nd);
  1504. if (!result)
  1505. result = handle_reval_path(nd);
  1506. if (result == -ESTALE) {
  1507. /* nd->path had been dropped */
  1508. current->total_link_count = 0;
  1509. nd->path.dentry = dentry;
  1510. nd->path.mnt = mnt;
  1511. nd->inode = dentry->d_inode;
  1512. path_get(&nd->path);
  1513. nd->flags = flags | LOOKUP_JUMPED | LOOKUP_REVAL;
  1514. result = link_path_walk(name, nd);
  1515. if (!result)
  1516. result = handle_reval_path(nd);
  1517. }
  1518. if (unlikely(!result && !audit_dummy_context() && nd->path.dentry &&
  1519. nd->inode))
  1520. audit_inode(name, nd->path.dentry);
  1521. path_put(&nd->root);
  1522. nd->root.mnt = NULL;
  1523. return result;
  1524. }
  1525. static struct dentry *__lookup_hash(struct qstr *name,
  1526. struct dentry *base, struct nameidata *nd)
  1527. {
  1528. struct inode *inode = base->d_inode;
  1529. struct dentry *dentry;
  1530. int err;
  1531. err = exec_permission(inode, 0);
  1532. if (err)
  1533. return ERR_PTR(err);
  1534. /*
  1535. * See if the low-level filesystem might want
  1536. * to use its own hash..
  1537. */
  1538. if (base->d_flags & DCACHE_OP_HASH) {
  1539. err = base->d_op->d_hash(base, inode, name);
  1540. dentry = ERR_PTR(err);
  1541. if (err < 0)
  1542. goto out;
  1543. }
  1544. /*
  1545. * Don't bother with __d_lookup: callers are for creat as
  1546. * well as unlink, so a lot of the time it would cost
  1547. * a double lookup.
  1548. */
  1549. dentry = d_lookup(base, name);
  1550. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
  1551. dentry = do_revalidate(dentry, nd);
  1552. if (!dentry)
  1553. dentry = d_alloc_and_lookup(base, name, nd);
  1554. out:
  1555. return dentry;
  1556. }
  1557. /*
  1558. * Restricted form of lookup. Doesn't follow links, single-component only,
  1559. * needs parent already locked. Doesn't follow mounts.
  1560. * SMP-safe.
  1561. */
  1562. static struct dentry *lookup_hash(struct nameidata *nd)
  1563. {
  1564. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1565. }
  1566. static int __lookup_one_len(const char *name, struct qstr *this,
  1567. struct dentry *base, int len)
  1568. {
  1569. unsigned long hash;
  1570. unsigned int c;
  1571. this->name = name;
  1572. this->len = len;
  1573. if (!len)
  1574. return -EACCES;
  1575. hash = init_name_hash();
  1576. while (len--) {
  1577. c = *(const unsigned char *)name++;
  1578. if (c == '/' || c == '\0')
  1579. return -EACCES;
  1580. hash = partial_name_hash(c, hash);
  1581. }
  1582. this->hash = end_name_hash(hash);
  1583. return 0;
  1584. }
  1585. /**
  1586. * lookup_one_len - filesystem helper to lookup single pathname component
  1587. * @name: pathname component to lookup
  1588. * @base: base directory to lookup from
  1589. * @len: maximum length @len should be interpreted to
  1590. *
  1591. * Note that this routine is purely a helper for filesystem usage and should
  1592. * not be called by generic code. Also note that by using this function the
  1593. * nameidata argument is passed to the filesystem methods and a filesystem
  1594. * using this helper needs to be prepared for that.
  1595. */
  1596. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1597. {
  1598. int err;
  1599. struct qstr this;
  1600. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1601. err = __lookup_one_len(name, &this, base, len);
  1602. if (err)
  1603. return ERR_PTR(err);
  1604. return __lookup_hash(&this, base, NULL);
  1605. }
  1606. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1607. struct path *path)
  1608. {
  1609. struct nameidata nd;
  1610. char *tmp = getname(name);
  1611. int err = PTR_ERR(tmp);
  1612. if (!IS_ERR(tmp)) {
  1613. BUG_ON(flags & LOOKUP_PARENT);
  1614. err = do_path_lookup(dfd, tmp, flags, &nd);
  1615. putname(tmp);
  1616. if (!err)
  1617. *path = nd.path;
  1618. }
  1619. return err;
  1620. }
  1621. static int user_path_parent(int dfd, const char __user *path,
  1622. struct nameidata *nd, char **name)
  1623. {
  1624. char *s = getname(path);
  1625. int error;
  1626. if (IS_ERR(s))
  1627. return PTR_ERR(s);
  1628. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1629. if (error)
  1630. putname(s);
  1631. else
  1632. *name = s;
  1633. return error;
  1634. }
  1635. /*
  1636. * It's inline, so penalty for filesystems that don't use sticky bit is
  1637. * minimal.
  1638. */
  1639. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1640. {
  1641. uid_t fsuid = current_fsuid();
  1642. if (!(dir->i_mode & S_ISVTX))
  1643. return 0;
  1644. if (inode->i_uid == fsuid)
  1645. return 0;
  1646. if (dir->i_uid == fsuid)
  1647. return 0;
  1648. return !capable(CAP_FOWNER);
  1649. }
  1650. /*
  1651. * Check whether we can remove a link victim from directory dir, check
  1652. * whether the type of victim is right.
  1653. * 1. We can't do it if dir is read-only (done in permission())
  1654. * 2. We should have write and exec permissions on dir
  1655. * 3. We can't remove anything from append-only dir
  1656. * 4. We can't do anything with immutable dir (done in permission())
  1657. * 5. If the sticky bit on dir is set we should either
  1658. * a. be owner of dir, or
  1659. * b. be owner of victim, or
  1660. * c. have CAP_FOWNER capability
  1661. * 6. If the victim is append-only or immutable we can't do antyhing with
  1662. * links pointing to it.
  1663. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1664. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1665. * 9. We can't remove a root or mountpoint.
  1666. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1667. * nfs_async_unlink().
  1668. */
  1669. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1670. {
  1671. int error;
  1672. if (!victim->d_inode)
  1673. return -ENOENT;
  1674. BUG_ON(victim->d_parent->d_inode != dir);
  1675. audit_inode_child(victim, dir);
  1676. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1677. if (error)
  1678. return error;
  1679. if (IS_APPEND(dir))
  1680. return -EPERM;
  1681. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1682. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1683. return -EPERM;
  1684. if (isdir) {
  1685. if (!S_ISDIR(victim->d_inode->i_mode))
  1686. return -ENOTDIR;
  1687. if (IS_ROOT(victim))
  1688. return -EBUSY;
  1689. } else if (S_ISDIR(victim->d_inode->i_mode))
  1690. return -EISDIR;
  1691. if (IS_DEADDIR(dir))
  1692. return -ENOENT;
  1693. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1694. return -EBUSY;
  1695. return 0;
  1696. }
  1697. /* Check whether we can create an object with dentry child in directory
  1698. * dir.
  1699. * 1. We can't do it if child already exists (open has special treatment for
  1700. * this case, but since we are inlined it's OK)
  1701. * 2. We can't do it if dir is read-only (done in permission())
  1702. * 3. We should have write and exec permissions on dir
  1703. * 4. We can't do it if dir is immutable (done in permission())
  1704. */
  1705. static inline int may_create(struct inode *dir, struct dentry *child)
  1706. {
  1707. if (child->d_inode)
  1708. return -EEXIST;
  1709. if (IS_DEADDIR(dir))
  1710. return -ENOENT;
  1711. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1712. }
  1713. /*
  1714. * p1 and p2 should be directories on the same fs.
  1715. */
  1716. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1717. {
  1718. struct dentry *p;
  1719. if (p1 == p2) {
  1720. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1721. return NULL;
  1722. }
  1723. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1724. p = d_ancestor(p2, p1);
  1725. if (p) {
  1726. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1727. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1728. return p;
  1729. }
  1730. p = d_ancestor(p1, p2);
  1731. if (p) {
  1732. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1733. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1734. return p;
  1735. }
  1736. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1737. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1738. return NULL;
  1739. }
  1740. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1741. {
  1742. mutex_unlock(&p1->d_inode->i_mutex);
  1743. if (p1 != p2) {
  1744. mutex_unlock(&p2->d_inode->i_mutex);
  1745. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1746. }
  1747. }
  1748. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1749. struct nameidata *nd)
  1750. {
  1751. int error = may_create(dir, dentry);
  1752. if (error)
  1753. return error;
  1754. if (!dir->i_op->create)
  1755. return -EACCES; /* shouldn't it be ENOSYS? */
  1756. mode &= S_IALLUGO;
  1757. mode |= S_IFREG;
  1758. error = security_inode_create(dir, dentry, mode);
  1759. if (error)
  1760. return error;
  1761. error = dir->i_op->create(dir, dentry, mode, nd);
  1762. if (!error)
  1763. fsnotify_create(dir, dentry);
  1764. return error;
  1765. }
  1766. int may_open(struct path *path, int acc_mode, int flag)
  1767. {
  1768. struct dentry *dentry = path->dentry;
  1769. struct inode *inode = dentry->d_inode;
  1770. int error;
  1771. if (!inode)
  1772. return -ENOENT;
  1773. switch (inode->i_mode & S_IFMT) {
  1774. case S_IFLNK:
  1775. return -ELOOP;
  1776. case S_IFDIR:
  1777. if (acc_mode & MAY_WRITE)
  1778. return -EISDIR;
  1779. break;
  1780. case S_IFBLK:
  1781. case S_IFCHR:
  1782. if (path->mnt->mnt_flags & MNT_NODEV)
  1783. return -EACCES;
  1784. /*FALLTHRU*/
  1785. case S_IFIFO:
  1786. case S_IFSOCK:
  1787. flag &= ~O_TRUNC;
  1788. break;
  1789. }
  1790. error = inode_permission(inode, acc_mode);
  1791. if (error)
  1792. return error;
  1793. /*
  1794. * An append-only file must be opened in append mode for writing.
  1795. */
  1796. if (IS_APPEND(inode)) {
  1797. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1798. return -EPERM;
  1799. if (flag & O_TRUNC)
  1800. return -EPERM;
  1801. }
  1802. /* O_NOATIME can only be set by the owner or superuser */
  1803. if (flag & O_NOATIME && !is_owner_or_cap(inode))
  1804. return -EPERM;
  1805. /*
  1806. * Ensure there are no outstanding leases on the file.
  1807. */
  1808. return break_lease(inode, flag);
  1809. }
  1810. static int handle_truncate(struct file *filp)
  1811. {
  1812. struct path *path = &filp->f_path;
  1813. struct inode *inode = path->dentry->d_inode;
  1814. int error = get_write_access(inode);
  1815. if (error)
  1816. return error;
  1817. /*
  1818. * Refuse to truncate files with mandatory locks held on them.
  1819. */
  1820. error = locks_verify_locked(inode);
  1821. if (!error)
  1822. error = security_path_truncate(path);
  1823. if (!error) {
  1824. error = do_truncate(path->dentry, 0,
  1825. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1826. filp);
  1827. }
  1828. put_write_access(inode);
  1829. return error;
  1830. }
  1831. /*
  1832. * Be careful about ever adding any more callers of this
  1833. * function. Its flags must be in the namei format, not
  1834. * what get passed to sys_open().
  1835. */
  1836. static int __open_namei_create(struct nameidata *nd, struct path *path,
  1837. int open_flag, int mode)
  1838. {
  1839. int error;
  1840. struct dentry *dir = nd->path.dentry;
  1841. if (!IS_POSIXACL(dir->d_inode))
  1842. mode &= ~current_umask();
  1843. error = security_path_mknod(&nd->path, path->dentry, mode, 0);
  1844. if (error)
  1845. goto out_unlock;
  1846. error = vfs_create(dir->d_inode, path->dentry, mode, nd);
  1847. out_unlock:
  1848. mutex_unlock(&dir->d_inode->i_mutex);
  1849. dput(nd->path.dentry);
  1850. nd->path.dentry = path->dentry;
  1851. if (error)
  1852. return error;
  1853. /* Don't check for write permission, don't truncate */
  1854. return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
  1855. }
  1856. /*
  1857. * Note that while the flag value (low two bits) for sys_open means:
  1858. * 00 - read-only
  1859. * 01 - write-only
  1860. * 10 - read-write
  1861. * 11 - special
  1862. * it is changed into
  1863. * 00 - no permissions needed
  1864. * 01 - read-permission
  1865. * 10 - write-permission
  1866. * 11 - read-write
  1867. * for the internal routines (ie open_namei()/follow_link() etc)
  1868. * This is more logical, and also allows the 00 "no perm needed"
  1869. * to be used for symlinks (where the permissions are checked
  1870. * later).
  1871. *
  1872. */
  1873. static inline int open_to_namei_flags(int flag)
  1874. {
  1875. if ((flag+1) & O_ACCMODE)
  1876. flag++;
  1877. return flag;
  1878. }
  1879. static int open_will_truncate(int flag, struct inode *inode)
  1880. {
  1881. /*
  1882. * We'll never write to the fs underlying
  1883. * a device file.
  1884. */
  1885. if (special_file(inode->i_mode))
  1886. return 0;
  1887. return (flag & O_TRUNC);
  1888. }
  1889. static struct file *finish_open(struct nameidata *nd,
  1890. int open_flag, int acc_mode)
  1891. {
  1892. struct file *filp;
  1893. int will_truncate;
  1894. int error;
  1895. will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
  1896. if (will_truncate) {
  1897. error = mnt_want_write(nd->path.mnt);
  1898. if (error)
  1899. goto exit;
  1900. }
  1901. error = may_open(&nd->path, acc_mode, open_flag);
  1902. if (error) {
  1903. if (will_truncate)
  1904. mnt_drop_write(nd->path.mnt);
  1905. goto exit;
  1906. }
  1907. filp = nameidata_to_filp(nd);
  1908. if (!IS_ERR(filp)) {
  1909. error = ima_file_check(filp, acc_mode);
  1910. if (error) {
  1911. fput(filp);
  1912. filp = ERR_PTR(error);
  1913. }
  1914. }
  1915. if (!IS_ERR(filp)) {
  1916. if (will_truncate) {
  1917. error = handle_truncate(filp);
  1918. if (error) {
  1919. fput(filp);
  1920. filp = ERR_PTR(error);
  1921. }
  1922. }
  1923. }
  1924. /*
  1925. * It is now safe to drop the mnt write
  1926. * because the filp has had a write taken
  1927. * on its behalf.
  1928. */
  1929. if (will_truncate)
  1930. mnt_drop_write(nd->path.mnt);
  1931. path_put(&nd->path);
  1932. return filp;
  1933. exit:
  1934. path_put(&nd->path);
  1935. return ERR_PTR(error);
  1936. }
  1937. /*
  1938. * Handle O_CREAT case for do_filp_open
  1939. */
  1940. static struct file *do_last(struct nameidata *nd, struct path *path,
  1941. const struct open_flags *op, const char *pathname)
  1942. {
  1943. struct dentry *dir = nd->path.dentry;
  1944. struct file *filp;
  1945. int error;
  1946. nd->flags &= ~LOOKUP_PARENT;
  1947. nd->flags |= op->intent;
  1948. switch (nd->last_type) {
  1949. case LAST_DOTDOT:
  1950. follow_dotdot(nd);
  1951. dir = nd->path.dentry;
  1952. case LAST_DOT:
  1953. /* fallthrough */
  1954. case LAST_ROOT:
  1955. error = handle_reval_path(nd);
  1956. if (error)
  1957. goto exit;
  1958. error = -EISDIR;
  1959. goto exit;
  1960. case LAST_BIND:
  1961. error = handle_reval_path(nd);
  1962. if (error)
  1963. goto exit;
  1964. audit_inode(pathname, dir);
  1965. goto ok;
  1966. }
  1967. error = -EISDIR;
  1968. /* trailing slashes? */
  1969. if (nd->last.name[nd->last.len])
  1970. goto exit;
  1971. mutex_lock(&dir->d_inode->i_mutex);
  1972. path->dentry = lookup_hash(nd);
  1973. path->mnt = nd->path.mnt;
  1974. error = PTR_ERR(path->dentry);
  1975. if (IS_ERR(path->dentry)) {
  1976. mutex_unlock(&dir->d_inode->i_mutex);
  1977. goto exit;
  1978. }
  1979. if (IS_ERR(nd->intent.open.file)) {
  1980. error = PTR_ERR(nd->intent.open.file);
  1981. goto exit_mutex_unlock;
  1982. }
  1983. /* Negative dentry, just create the file */
  1984. if (!path->dentry->d_inode) {
  1985. /*
  1986. * This write is needed to ensure that a
  1987. * ro->rw transition does not occur between
  1988. * the time when the file is created and when
  1989. * a permanent write count is taken through
  1990. * the 'struct file' in nameidata_to_filp().
  1991. */
  1992. error = mnt_want_write(nd->path.mnt);
  1993. if (error)
  1994. goto exit_mutex_unlock;
  1995. error = __open_namei_create(nd, path, op->open_flag, op->mode);
  1996. if (error) {
  1997. mnt_drop_write(nd->path.mnt);
  1998. goto exit;
  1999. }
  2000. filp = nameidata_to_filp(nd);
  2001. mnt_drop_write(nd->path.mnt);
  2002. path_put(&nd->path);
  2003. if (!IS_ERR(filp)) {
  2004. error = ima_file_check(filp, op->acc_mode);
  2005. if (error) {
  2006. fput(filp);
  2007. filp = ERR_PTR(error);
  2008. }
  2009. }
  2010. return filp;
  2011. }
  2012. /*
  2013. * It already exists.
  2014. */
  2015. mutex_unlock(&dir->d_inode->i_mutex);
  2016. audit_inode(pathname, path->dentry);
  2017. error = -EEXIST;
  2018. if (op->open_flag & O_EXCL)
  2019. goto exit_dput;
  2020. error = follow_managed(path, nd->flags);
  2021. if (error < 0)
  2022. goto exit_dput;
  2023. error = -ENOENT;
  2024. if (!path->dentry->d_inode)
  2025. goto exit_dput;
  2026. if (path->dentry->d_inode->i_op->follow_link)
  2027. return NULL;
  2028. path_to_nameidata(path, nd);
  2029. nd->inode = path->dentry->d_inode;
  2030. error = -EISDIR;
  2031. if (S_ISDIR(nd->inode->i_mode))
  2032. goto exit;
  2033. ok:
  2034. filp = finish_open(nd, op->open_flag, op->acc_mode);
  2035. return filp;
  2036. exit_mutex_unlock:
  2037. mutex_unlock(&dir->d_inode->i_mutex);
  2038. exit_dput:
  2039. path_put_conditional(path, nd);
  2040. exit:
  2041. path_put(&nd->path);
  2042. return ERR_PTR(error);
  2043. }
  2044. static struct file *path_openat(int dfd, const char *pathname,
  2045. const struct open_flags *op, int flags)
  2046. {
  2047. struct file *filp;
  2048. struct nameidata nd;
  2049. struct path path;
  2050. int count = 0;
  2051. int error;
  2052. filp = get_empty_filp();
  2053. if (!filp)
  2054. return ERR_PTR(-ENFILE);
  2055. filp->f_flags = op->open_flag;
  2056. nd.intent.open.file = filp;
  2057. nd.intent.open.flags = open_to_namei_flags(op->open_flag);
  2058. nd.intent.open.create_mode = op->mode;
  2059. if (op->open_flag & O_CREAT)
  2060. goto creat;
  2061. /* !O_CREAT, simple open */
  2062. error = path_lookupat(dfd, pathname, flags | op->intent, &nd);
  2063. if (unlikely(error))
  2064. goto out_filp;
  2065. error = -ELOOP;
  2066. if (!(nd.flags & LOOKUP_FOLLOW)) {
  2067. if (nd.inode->i_op->follow_link)
  2068. goto out_path;
  2069. }
  2070. error = -ENOTDIR;
  2071. if (nd.flags & LOOKUP_DIRECTORY) {
  2072. if (!nd.inode->i_op->lookup)
  2073. goto out_path;
  2074. }
  2075. audit_inode(pathname, nd.path.dentry);
  2076. filp = finish_open(&nd, op->open_flag, op->acc_mode);
  2077. release_open_intent(&nd);
  2078. return filp;
  2079. creat:
  2080. /* OK, have to create the file. Find the parent. */
  2081. error = path_lookupat(dfd, pathname, LOOKUP_PARENT | flags, &nd);
  2082. if (unlikely(error))
  2083. goto out_filp;
  2084. if (unlikely(!audit_dummy_context()))
  2085. audit_inode(pathname, nd.path.dentry);
  2086. /*
  2087. * We have the parent and last component.
  2088. */
  2089. filp = do_last(&nd, &path, op, pathname);
  2090. while (unlikely(!filp)) { /* trailing symlink */
  2091. struct path link = path;
  2092. struct inode *linki = link.dentry->d_inode;
  2093. void *cookie;
  2094. error = -ELOOP;
  2095. if (!(nd.flags & LOOKUP_FOLLOW))
  2096. goto exit_dput;
  2097. if (count++ == 32)
  2098. goto exit_dput;
  2099. /*
  2100. * This is subtle. Instead of calling do_follow_link() we do
  2101. * the thing by hands. The reason is that this way we have zero
  2102. * link_count and path_walk() (called from ->follow_link)
  2103. * honoring LOOKUP_PARENT. After that we have the parent and
  2104. * last component, i.e. we are in the same situation as after
  2105. * the first path_walk(). Well, almost - if the last component
  2106. * is normal we get its copy stored in nd->last.name and we will
  2107. * have to putname() it when we are done. Procfs-like symlinks
  2108. * just set LAST_BIND.
  2109. */
  2110. nd.flags |= LOOKUP_PARENT;
  2111. nd.flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2112. error = __do_follow_link(&link, &nd, &cookie);
  2113. if (unlikely(error))
  2114. filp = ERR_PTR(error);
  2115. else
  2116. filp = do_last(&nd, &path, op, pathname);
  2117. if (!IS_ERR(cookie) && linki->i_op->put_link)
  2118. linki->i_op->put_link(link.dentry, &nd, cookie);
  2119. path_put(&link);
  2120. }
  2121. out:
  2122. if (nd.root.mnt)
  2123. path_put(&nd.root);
  2124. release_open_intent(&nd);
  2125. return filp;
  2126. exit_dput:
  2127. path_put_conditional(&path, &nd);
  2128. out_path:
  2129. path_put(&nd.path);
  2130. out_filp:
  2131. filp = ERR_PTR(error);
  2132. goto out;
  2133. }
  2134. struct file *do_filp_open(int dfd, const char *pathname,
  2135. const struct open_flags *op, int flags)
  2136. {
  2137. struct file *filp;
  2138. filp = path_openat(dfd, pathname, op, flags | LOOKUP_RCU);
  2139. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2140. filp = path_openat(dfd, pathname, op, flags);
  2141. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2142. filp = path_openat(dfd, pathname, op, flags | LOOKUP_REVAL);
  2143. return filp;
  2144. }
  2145. /**
  2146. * lookup_create - lookup a dentry, creating it if it doesn't exist
  2147. * @nd: nameidata info
  2148. * @is_dir: directory flag
  2149. *
  2150. * Simple function to lookup and return a dentry and create it
  2151. * if it doesn't exist. Is SMP-safe.
  2152. *
  2153. * Returns with nd->path.dentry->d_inode->i_mutex locked.
  2154. */
  2155. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  2156. {
  2157. struct dentry *dentry = ERR_PTR(-EEXIST);
  2158. mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2159. /*
  2160. * Yucky last component or no last component at all?
  2161. * (foo/., foo/.., /////)
  2162. */
  2163. if (nd->last_type != LAST_NORM)
  2164. goto fail;
  2165. nd->flags &= ~LOOKUP_PARENT;
  2166. nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2167. nd->intent.open.flags = O_EXCL;
  2168. /*
  2169. * Do the final lookup.
  2170. */
  2171. dentry = lookup_hash(nd);
  2172. if (IS_ERR(dentry))
  2173. goto fail;
  2174. if (dentry->d_inode)
  2175. goto eexist;
  2176. /*
  2177. * Special case - lookup gave negative, but... we had foo/bar/
  2178. * From the vfs_mknod() POV we just have a negative dentry -
  2179. * all is fine. Let's be bastards - you had / on the end, you've
  2180. * been asking for (non-existent) directory. -ENOENT for you.
  2181. */
  2182. if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
  2183. dput(dentry);
  2184. dentry = ERR_PTR(-ENOENT);
  2185. }
  2186. return dentry;
  2187. eexist:
  2188. dput(dentry);
  2189. dentry = ERR_PTR(-EEXIST);
  2190. fail:
  2191. return dentry;
  2192. }
  2193. EXPORT_SYMBOL_GPL(lookup_create);
  2194. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  2195. {
  2196. int error = may_create(dir, dentry);
  2197. if (error)
  2198. return error;
  2199. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  2200. return -EPERM;
  2201. if (!dir->i_op->mknod)
  2202. return -EPERM;
  2203. error = devcgroup_inode_mknod(mode, dev);
  2204. if (error)
  2205. return error;
  2206. error = security_inode_mknod(dir, dentry, mode, dev);
  2207. if (error)
  2208. return error;
  2209. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2210. if (!error)
  2211. fsnotify_create(dir, dentry);
  2212. return error;
  2213. }
  2214. static int may_mknod(mode_t mode)
  2215. {
  2216. switch (mode & S_IFMT) {
  2217. case S_IFREG:
  2218. case S_IFCHR:
  2219. case S_IFBLK:
  2220. case S_IFIFO:
  2221. case S_IFSOCK:
  2222. case 0: /* zero mode translates to S_IFREG */
  2223. return 0;
  2224. case S_IFDIR:
  2225. return -EPERM;
  2226. default:
  2227. return -EINVAL;
  2228. }
  2229. }
  2230. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
  2231. unsigned, dev)
  2232. {
  2233. int error;
  2234. char *tmp;
  2235. struct dentry *dentry;
  2236. struct nameidata nd;
  2237. if (S_ISDIR(mode))
  2238. return -EPERM;
  2239. error = user_path_parent(dfd, filename, &nd, &tmp);
  2240. if (error)
  2241. return error;
  2242. dentry = lookup_create(&nd, 0);
  2243. if (IS_ERR(dentry)) {
  2244. error = PTR_ERR(dentry);
  2245. goto out_unlock;
  2246. }
  2247. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2248. mode &= ~current_umask();
  2249. error = may_mknod(mode);
  2250. if (error)
  2251. goto out_dput;
  2252. error = mnt_want_write(nd.path.mnt);
  2253. if (error)
  2254. goto out_dput;
  2255. error = security_path_mknod(&nd.path, dentry, mode, dev);
  2256. if (error)
  2257. goto out_drop_write;
  2258. switch (mode & S_IFMT) {
  2259. case 0: case S_IFREG:
  2260. error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
  2261. break;
  2262. case S_IFCHR: case S_IFBLK:
  2263. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
  2264. new_decode_dev(dev));
  2265. break;
  2266. case S_IFIFO: case S_IFSOCK:
  2267. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
  2268. break;
  2269. }
  2270. out_drop_write:
  2271. mnt_drop_write(nd.path.mnt);
  2272. out_dput:
  2273. dput(dentry);
  2274. out_unlock:
  2275. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2276. path_put(&nd.path);
  2277. putname(tmp);
  2278. return error;
  2279. }
  2280. SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
  2281. {
  2282. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2283. }
  2284. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2285. {
  2286. int error = may_create(dir, dentry);
  2287. if (error)
  2288. return error;
  2289. if (!dir->i_op->mkdir)
  2290. return -EPERM;
  2291. mode &= (S_IRWXUGO|S_ISVTX);
  2292. error = security_inode_mkdir(dir, dentry, mode);
  2293. if (error)
  2294. return error;
  2295. error = dir->i_op->mkdir(dir, dentry, mode);
  2296. if (!error)
  2297. fsnotify_mkdir(dir, dentry);
  2298. return error;
  2299. }
  2300. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
  2301. {
  2302. int error = 0;
  2303. char * tmp;
  2304. struct dentry *dentry;
  2305. struct nameidata nd;
  2306. error = user_path_parent(dfd, pathname, &nd, &tmp);
  2307. if (error)
  2308. goto out_err;
  2309. dentry = lookup_create(&nd, 1);
  2310. error = PTR_ERR(dentry);
  2311. if (IS_ERR(dentry))
  2312. goto out_unlock;
  2313. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2314. mode &= ~current_umask();
  2315. error = mnt_want_write(nd.path.mnt);
  2316. if (error)
  2317. goto out_dput;
  2318. error = security_path_mkdir(&nd.path, dentry, mode);
  2319. if (error)
  2320. goto out_drop_write;
  2321. error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
  2322. out_drop_write:
  2323. mnt_drop_write(nd.path.mnt);
  2324. out_dput:
  2325. dput(dentry);
  2326. out_unlock:
  2327. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2328. path_put(&nd.path);
  2329. putname(tmp);
  2330. out_err:
  2331. return error;
  2332. }
  2333. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
  2334. {
  2335. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2336. }
  2337. /*
  2338. * We try to drop the dentry early: we should have
  2339. * a usage count of 2 if we're the only user of this
  2340. * dentry, and if that is true (possibly after pruning
  2341. * the dcache), then we drop the dentry now.
  2342. *
  2343. * A low-level filesystem can, if it choses, legally
  2344. * do a
  2345. *
  2346. * if (!d_unhashed(dentry))
  2347. * return -EBUSY;
  2348. *
  2349. * if it cannot handle the case of removing a directory
  2350. * that is still in use by something else..
  2351. */
  2352. void dentry_unhash(struct dentry *dentry)
  2353. {
  2354. dget(dentry);
  2355. shrink_dcache_parent(dentry);
  2356. spin_lock(&dentry->d_lock);
  2357. if (dentry->d_count == 2)
  2358. __d_drop(dentry);
  2359. spin_unlock(&dentry->d_lock);
  2360. }
  2361. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2362. {
  2363. int error = may_delete(dir, dentry, 1);
  2364. if (error)
  2365. return error;
  2366. if (!dir->i_op->rmdir)
  2367. return -EPERM;
  2368. mutex_lock(&dentry->d_inode->i_mutex);
  2369. dentry_unhash(dentry);
  2370. if (d_mountpoint(dentry))
  2371. error = -EBUSY;
  2372. else {
  2373. error = security_inode_rmdir(dir, dentry);
  2374. if (!error) {
  2375. error = dir->i_op->rmdir(dir, dentry);
  2376. if (!error) {
  2377. dentry->d_inode->i_flags |= S_DEAD;
  2378. dont_mount(dentry);
  2379. }
  2380. }
  2381. }
  2382. mutex_unlock(&dentry->d_inode->i_mutex);
  2383. if (!error) {
  2384. d_delete(dentry);
  2385. }
  2386. dput(dentry);
  2387. return error;
  2388. }
  2389. static long do_rmdir(int dfd, const char __user *pathname)
  2390. {
  2391. int error = 0;
  2392. char * name;
  2393. struct dentry *dentry;
  2394. struct nameidata nd;
  2395. error = user_path_parent(dfd, pathname, &nd, &name);
  2396. if (error)
  2397. return error;
  2398. switch(nd.last_type) {
  2399. case LAST_DOTDOT:
  2400. error = -ENOTEMPTY;
  2401. goto exit1;
  2402. case LAST_DOT:
  2403. error = -EINVAL;
  2404. goto exit1;
  2405. case LAST_ROOT:
  2406. error = -EBUSY;
  2407. goto exit1;
  2408. }
  2409. nd.flags &= ~LOOKUP_PARENT;
  2410. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2411. dentry = lookup_hash(&nd);
  2412. error = PTR_ERR(dentry);
  2413. if (IS_ERR(dentry))
  2414. goto exit2;
  2415. error = mnt_want_write(nd.path.mnt);
  2416. if (error)
  2417. goto exit3;
  2418. error = security_path_rmdir(&nd.path, dentry);
  2419. if (error)
  2420. goto exit4;
  2421. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2422. exit4:
  2423. mnt_drop_write(nd.path.mnt);
  2424. exit3:
  2425. dput(dentry);
  2426. exit2:
  2427. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2428. exit1:
  2429. path_put(&nd.path);
  2430. putname(name);
  2431. return error;
  2432. }
  2433. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2434. {
  2435. return do_rmdir(AT_FDCWD, pathname);
  2436. }
  2437. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2438. {
  2439. int error = may_delete(dir, dentry, 0);
  2440. if (error)
  2441. return error;
  2442. if (!dir->i_op->unlink)
  2443. return -EPERM;
  2444. mutex_lock(&dentry->d_inode->i_mutex);
  2445. if (d_mountpoint(dentry))
  2446. error = -EBUSY;
  2447. else {
  2448. error = security_inode_unlink(dir, dentry);
  2449. if (!error) {
  2450. error = dir->i_op->unlink(dir, dentry);
  2451. if (!error)
  2452. dont_mount(dentry);
  2453. }
  2454. }
  2455. mutex_unlock(&dentry->d_inode->i_mutex);
  2456. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2457. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2458. fsnotify_link_count(dentry->d_inode);
  2459. d_delete(dentry);
  2460. }
  2461. return error;
  2462. }
  2463. /*
  2464. * Make sure that the actual truncation of the file will occur outside its
  2465. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2466. * writeout happening, and we don't want to prevent access to the directory
  2467. * while waiting on the I/O.
  2468. */
  2469. static long do_unlinkat(int dfd, const char __user *pathname)
  2470. {
  2471. int error;
  2472. char *name;
  2473. struct dentry *dentry;
  2474. struct nameidata nd;
  2475. struct inode *inode = NULL;
  2476. error = user_path_parent(dfd, pathname, &nd, &name);
  2477. if (error)
  2478. return error;
  2479. error = -EISDIR;
  2480. if (nd.last_type != LAST_NORM)
  2481. goto exit1;
  2482. nd.flags &= ~LOOKUP_PARENT;
  2483. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2484. dentry = lookup_hash(&nd);
  2485. error = PTR_ERR(dentry);
  2486. if (!IS_ERR(dentry)) {
  2487. /* Why not before? Because we want correct error value */
  2488. if (nd.last.name[nd.last.len])
  2489. goto slashes;
  2490. inode = dentry->d_inode;
  2491. if (inode)
  2492. ihold(inode);
  2493. error = mnt_want_write(nd.path.mnt);
  2494. if (error)
  2495. goto exit2;
  2496. error = security_path_unlink(&nd.path, dentry);
  2497. if (error)
  2498. goto exit3;
  2499. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2500. exit3:
  2501. mnt_drop_write(nd.path.mnt);
  2502. exit2:
  2503. dput(dentry);
  2504. }
  2505. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2506. if (inode)
  2507. iput(inode); /* truncate the inode here */
  2508. exit1:
  2509. path_put(&nd.path);
  2510. putname(name);
  2511. return error;
  2512. slashes:
  2513. error = !dentry->d_inode ? -ENOENT :
  2514. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2515. goto exit2;
  2516. }
  2517. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2518. {
  2519. if ((flag & ~AT_REMOVEDIR) != 0)
  2520. return -EINVAL;
  2521. if (flag & AT_REMOVEDIR)
  2522. return do_rmdir(dfd, pathname);
  2523. return do_unlinkat(dfd, pathname);
  2524. }
  2525. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2526. {
  2527. return do_unlinkat(AT_FDCWD, pathname);
  2528. }
  2529. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2530. {
  2531. int error = may_create(dir, dentry);
  2532. if (error)
  2533. return error;
  2534. if (!dir->i_op->symlink)
  2535. return -EPERM;
  2536. error = security_inode_symlink(dir, dentry, oldname);
  2537. if (error)
  2538. return error;
  2539. error = dir->i_op->symlink(dir, dentry, oldname);
  2540. if (!error)
  2541. fsnotify_create(dir, dentry);
  2542. return error;
  2543. }
  2544. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2545. int, newdfd, const char __user *, newname)
  2546. {
  2547. int error;
  2548. char *from;
  2549. char *to;
  2550. struct dentry *dentry;
  2551. struct nameidata nd;
  2552. from = getname(oldname);
  2553. if (IS_ERR(from))
  2554. return PTR_ERR(from);
  2555. error = user_path_parent(newdfd, newname, &nd, &to);
  2556. if (error)
  2557. goto out_putname;
  2558. dentry = lookup_create(&nd, 0);
  2559. error = PTR_ERR(dentry);
  2560. if (IS_ERR(dentry))
  2561. goto out_unlock;
  2562. error = mnt_want_write(nd.path.mnt);
  2563. if (error)
  2564. goto out_dput;
  2565. error = security_path_symlink(&nd.path, dentry, from);
  2566. if (error)
  2567. goto out_drop_write;
  2568. error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
  2569. out_drop_write:
  2570. mnt_drop_write(nd.path.mnt);
  2571. out_dput:
  2572. dput(dentry);
  2573. out_unlock:
  2574. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2575. path_put(&nd.path);
  2576. putname(to);
  2577. out_putname:
  2578. putname(from);
  2579. return error;
  2580. }
  2581. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2582. {
  2583. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2584. }
  2585. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2586. {
  2587. struct inode *inode = old_dentry->d_inode;
  2588. int error;
  2589. if (!inode)
  2590. return -ENOENT;
  2591. error = may_create(dir, new_dentry);
  2592. if (error)
  2593. return error;
  2594. if (dir->i_sb != inode->i_sb)
  2595. return -EXDEV;
  2596. /*
  2597. * A link to an append-only or immutable file cannot be created.
  2598. */
  2599. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2600. return -EPERM;
  2601. if (!dir->i_op->link)
  2602. return -EPERM;
  2603. if (S_ISDIR(inode->i_mode))
  2604. return -EPERM;
  2605. error = security_inode_link(old_dentry, dir, new_dentry);
  2606. if (error)
  2607. return error;
  2608. mutex_lock(&inode->i_mutex);
  2609. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2610. mutex_unlock(&inode->i_mutex);
  2611. if (!error)
  2612. fsnotify_link(dir, inode, new_dentry);
  2613. return error;
  2614. }
  2615. /*
  2616. * Hardlinks are often used in delicate situations. We avoid
  2617. * security-related surprises by not following symlinks on the
  2618. * newname. --KAB
  2619. *
  2620. * We don't follow them on the oldname either to be compatible
  2621. * with linux 2.0, and to avoid hard-linking to directories
  2622. * and other special files. --ADM
  2623. */
  2624. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2625. int, newdfd, const char __user *, newname, int, flags)
  2626. {
  2627. struct dentry *new_dentry;
  2628. struct nameidata nd;
  2629. struct path old_path;
  2630. int error;
  2631. char *to;
  2632. if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
  2633. return -EINVAL;
  2634. error = user_path_at(olddfd, oldname,
  2635. flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
  2636. &old_path);
  2637. if (error)
  2638. return error;
  2639. error = user_path_parent(newdfd, newname, &nd, &to);
  2640. if (error)
  2641. goto out;
  2642. error = -EXDEV;
  2643. if (old_path.mnt != nd.path.mnt)
  2644. goto out_release;
  2645. new_dentry = lookup_create(&nd, 0);
  2646. error = PTR_ERR(new_dentry);
  2647. if (IS_ERR(new_dentry))
  2648. goto out_unlock;
  2649. error = mnt_want_write(nd.path.mnt);
  2650. if (error)
  2651. goto out_dput;
  2652. error = security_path_link(old_path.dentry, &nd.path, new_dentry);
  2653. if (error)
  2654. goto out_drop_write;
  2655. error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
  2656. out_drop_write:
  2657. mnt_drop_write(nd.path.mnt);
  2658. out_dput:
  2659. dput(new_dentry);
  2660. out_unlock:
  2661. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2662. out_release:
  2663. path_put(&nd.path);
  2664. putname(to);
  2665. out:
  2666. path_put(&old_path);
  2667. return error;
  2668. }
  2669. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2670. {
  2671. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2672. }
  2673. /*
  2674. * The worst of all namespace operations - renaming directory. "Perverted"
  2675. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2676. * Problems:
  2677. * a) we can get into loop creation. Check is done in is_subdir().
  2678. * b) race potential - two innocent renames can create a loop together.
  2679. * That's where 4.4 screws up. Current fix: serialization on
  2680. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2681. * story.
  2682. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2683. * And that - after we got ->i_mutex on parents (until then we don't know
  2684. * whether the target exists). Solution: try to be smart with locking
  2685. * order for inodes. We rely on the fact that tree topology may change
  2686. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2687. * move will be locked. Thus we can rank directories by the tree
  2688. * (ancestors first) and rank all non-directories after them.
  2689. * That works since everybody except rename does "lock parent, lookup,
  2690. * lock child" and rename is under ->s_vfs_rename_mutex.
  2691. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2692. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2693. * we'd better make sure that there's no link(2) for them.
  2694. * d) some filesystems don't support opened-but-unlinked directories,
  2695. * either because of layout or because they are not ready to deal with
  2696. * all cases correctly. The latter will be fixed (taking this sort of
  2697. * stuff into VFS), but the former is not going away. Solution: the same
  2698. * trick as in rmdir().
  2699. * e) conversion from fhandle to dentry may come in the wrong moment - when
  2700. * we are removing the target. Solution: we will have to grab ->i_mutex
  2701. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2702. * ->i_mutex on parents, which works but leads to some truly excessive
  2703. * locking].
  2704. */
  2705. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2706. struct inode *new_dir, struct dentry *new_dentry)
  2707. {
  2708. int error = 0;
  2709. struct inode *target;
  2710. /*
  2711. * If we are going to change the parent - check write permissions,
  2712. * we'll need to flip '..'.
  2713. */
  2714. if (new_dir != old_dir) {
  2715. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2716. if (error)
  2717. return error;
  2718. }
  2719. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2720. if (error)
  2721. return error;
  2722. target = new_dentry->d_inode;
  2723. if (target)
  2724. mutex_lock(&target->i_mutex);
  2725. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2726. error = -EBUSY;
  2727. else {
  2728. if (target)
  2729. dentry_unhash(new_dentry);
  2730. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2731. }
  2732. if (target) {
  2733. if (!error) {
  2734. target->i_flags |= S_DEAD;
  2735. dont_mount(new_dentry);
  2736. }
  2737. mutex_unlock(&target->i_mutex);
  2738. if (d_unhashed(new_dentry))
  2739. d_rehash(new_dentry);
  2740. dput(new_dentry);
  2741. }
  2742. if (!error)
  2743. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2744. d_move(old_dentry,new_dentry);
  2745. return error;
  2746. }
  2747. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2748. struct inode *new_dir, struct dentry *new_dentry)
  2749. {
  2750. struct inode *target;
  2751. int error;
  2752. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2753. if (error)
  2754. return error;
  2755. dget(new_dentry);
  2756. target = new_dentry->d_inode;
  2757. if (target)
  2758. mutex_lock(&target->i_mutex);
  2759. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2760. error = -EBUSY;
  2761. else
  2762. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2763. if (!error) {
  2764. if (target)
  2765. dont_mount(new_dentry);
  2766. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2767. d_move(old_dentry, new_dentry);
  2768. }
  2769. if (target)
  2770. mutex_unlock(&target->i_mutex);
  2771. dput(new_dentry);
  2772. return error;
  2773. }
  2774. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2775. struct inode *new_dir, struct dentry *new_dentry)
  2776. {
  2777. int error;
  2778. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2779. const unsigned char *old_name;
  2780. if (old_dentry->d_inode == new_dentry->d_inode)
  2781. return 0;
  2782. error = may_delete(old_dir, old_dentry, is_dir);
  2783. if (error)
  2784. return error;
  2785. if (!new_dentry->d_inode)
  2786. error = may_create(new_dir, new_dentry);
  2787. else
  2788. error = may_delete(new_dir, new_dentry, is_dir);
  2789. if (error)
  2790. return error;
  2791. if (!old_dir->i_op->rename)
  2792. return -EPERM;
  2793. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2794. if (is_dir)
  2795. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2796. else
  2797. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2798. if (!error)
  2799. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2800. new_dentry->d_inode, old_dentry);
  2801. fsnotify_oldname_free(old_name);
  2802. return error;
  2803. }
  2804. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2805. int, newdfd, const char __user *, newname)
  2806. {
  2807. struct dentry *old_dir, *new_dir;
  2808. struct dentry *old_dentry, *new_dentry;
  2809. struct dentry *trap;
  2810. struct nameidata oldnd, newnd;
  2811. char *from;
  2812. char *to;
  2813. int error;
  2814. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2815. if (error)
  2816. goto exit;
  2817. error = user_path_parent(newdfd, newname, &newnd, &to);
  2818. if (error)
  2819. goto exit1;
  2820. error = -EXDEV;
  2821. if (oldnd.path.mnt != newnd.path.mnt)
  2822. goto exit2;
  2823. old_dir = oldnd.path.dentry;
  2824. error = -EBUSY;
  2825. if (oldnd.last_type != LAST_NORM)
  2826. goto exit2;
  2827. new_dir = newnd.path.dentry;
  2828. if (newnd.last_type != LAST_NORM)
  2829. goto exit2;
  2830. oldnd.flags &= ~LOOKUP_PARENT;
  2831. newnd.flags &= ~LOOKUP_PARENT;
  2832. newnd.flags |= LOOKUP_RENAME_TARGET;
  2833. trap = lock_rename(new_dir, old_dir);
  2834. old_dentry = lookup_hash(&oldnd);
  2835. error = PTR_ERR(old_dentry);
  2836. if (IS_ERR(old_dentry))
  2837. goto exit3;
  2838. /* source must exist */
  2839. error = -ENOENT;
  2840. if (!old_dentry->d_inode)
  2841. goto exit4;
  2842. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2843. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2844. error = -ENOTDIR;
  2845. if (oldnd.last.name[oldnd.last.len])
  2846. goto exit4;
  2847. if (newnd.last.name[newnd.last.len])
  2848. goto exit4;
  2849. }
  2850. /* source should not be ancestor of target */
  2851. error = -EINVAL;
  2852. if (old_dentry == trap)
  2853. goto exit4;
  2854. new_dentry = lookup_hash(&newnd);
  2855. error = PTR_ERR(new_dentry);
  2856. if (IS_ERR(new_dentry))
  2857. goto exit4;
  2858. /* target should not be an ancestor of source */
  2859. error = -ENOTEMPTY;
  2860. if (new_dentry == trap)
  2861. goto exit5;
  2862. error = mnt_want_write(oldnd.path.mnt);
  2863. if (error)
  2864. goto exit5;
  2865. error = security_path_rename(&oldnd.path, old_dentry,
  2866. &newnd.path, new_dentry);
  2867. if (error)
  2868. goto exit6;
  2869. error = vfs_rename(old_dir->d_inode, old_dentry,
  2870. new_dir->d_inode, new_dentry);
  2871. exit6:
  2872. mnt_drop_write(oldnd.path.mnt);
  2873. exit5:
  2874. dput(new_dentry);
  2875. exit4:
  2876. dput(old_dentry);
  2877. exit3:
  2878. unlock_rename(new_dir, old_dir);
  2879. exit2:
  2880. path_put(&newnd.path);
  2881. putname(to);
  2882. exit1:
  2883. path_put(&oldnd.path);
  2884. putname(from);
  2885. exit:
  2886. return error;
  2887. }
  2888. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2889. {
  2890. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2891. }
  2892. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2893. {
  2894. int len;
  2895. len = PTR_ERR(link);
  2896. if (IS_ERR(link))
  2897. goto out;
  2898. len = strlen(link);
  2899. if (len > (unsigned) buflen)
  2900. len = buflen;
  2901. if (copy_to_user(buffer, link, len))
  2902. len = -EFAULT;
  2903. out:
  2904. return len;
  2905. }
  2906. /*
  2907. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2908. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2909. * using) it for any given inode is up to filesystem.
  2910. */
  2911. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2912. {
  2913. struct nameidata nd;
  2914. void *cookie;
  2915. int res;
  2916. nd.depth = 0;
  2917. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2918. if (IS_ERR(cookie))
  2919. return PTR_ERR(cookie);
  2920. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2921. if (dentry->d_inode->i_op->put_link)
  2922. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2923. return res;
  2924. }
  2925. int vfs_follow_link(struct nameidata *nd, const char *link)
  2926. {
  2927. return __vfs_follow_link(nd, link);
  2928. }
  2929. /* get the link contents into pagecache */
  2930. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2931. {
  2932. char *kaddr;
  2933. struct page *page;
  2934. struct address_space *mapping = dentry->d_inode->i_mapping;
  2935. page = read_mapping_page(mapping, 0, NULL);
  2936. if (IS_ERR(page))
  2937. return (char*)page;
  2938. *ppage = page;
  2939. kaddr = kmap(page);
  2940. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  2941. return kaddr;
  2942. }
  2943. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2944. {
  2945. struct page *page = NULL;
  2946. char *s = page_getlink(dentry, &page);
  2947. int res = vfs_readlink(dentry,buffer,buflen,s);
  2948. if (page) {
  2949. kunmap(page);
  2950. page_cache_release(page);
  2951. }
  2952. return res;
  2953. }
  2954. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2955. {
  2956. struct page *page = NULL;
  2957. nd_set_link(nd, page_getlink(dentry, &page));
  2958. return page;
  2959. }
  2960. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2961. {
  2962. struct page *page = cookie;
  2963. if (page) {
  2964. kunmap(page);
  2965. page_cache_release(page);
  2966. }
  2967. }
  2968. /*
  2969. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  2970. */
  2971. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  2972. {
  2973. struct address_space *mapping = inode->i_mapping;
  2974. struct page *page;
  2975. void *fsdata;
  2976. int err;
  2977. char *kaddr;
  2978. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  2979. if (nofs)
  2980. flags |= AOP_FLAG_NOFS;
  2981. retry:
  2982. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2983. flags, &page, &fsdata);
  2984. if (err)
  2985. goto fail;
  2986. kaddr = kmap_atomic(page, KM_USER0);
  2987. memcpy(kaddr, symname, len-1);
  2988. kunmap_atomic(kaddr, KM_USER0);
  2989. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2990. page, fsdata);
  2991. if (err < 0)
  2992. goto fail;
  2993. if (err < len-1)
  2994. goto retry;
  2995. mark_inode_dirty(inode);
  2996. return 0;
  2997. fail:
  2998. return err;
  2999. }
  3000. int page_symlink(struct inode *inode, const char *symname, int len)
  3001. {
  3002. return __page_symlink(inode, symname, len,
  3003. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  3004. }
  3005. const struct inode_operations page_symlink_inode_operations = {
  3006. .readlink = generic_readlink,
  3007. .follow_link = page_follow_link_light,
  3008. .put_link = page_put_link,
  3009. };
  3010. EXPORT_SYMBOL(user_path_at);
  3011. EXPORT_SYMBOL(follow_down_one);
  3012. EXPORT_SYMBOL(follow_down);
  3013. EXPORT_SYMBOL(follow_up);
  3014. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  3015. EXPORT_SYMBOL(getname);
  3016. EXPORT_SYMBOL(lock_rename);
  3017. EXPORT_SYMBOL(lookup_one_len);
  3018. EXPORT_SYMBOL(page_follow_link_light);
  3019. EXPORT_SYMBOL(page_put_link);
  3020. EXPORT_SYMBOL(page_readlink);
  3021. EXPORT_SYMBOL(__page_symlink);
  3022. EXPORT_SYMBOL(page_symlink);
  3023. EXPORT_SYMBOL(page_symlink_inode_operations);
  3024. EXPORT_SYMBOL(kern_path_parent);
  3025. EXPORT_SYMBOL(kern_path);
  3026. EXPORT_SYMBOL(vfs_path_lookup);
  3027. EXPORT_SYMBOL(inode_permission);
  3028. EXPORT_SYMBOL(file_permission);
  3029. EXPORT_SYMBOL(unlock_rename);
  3030. EXPORT_SYMBOL(vfs_create);
  3031. EXPORT_SYMBOL(vfs_follow_link);
  3032. EXPORT_SYMBOL(vfs_link);
  3033. EXPORT_SYMBOL(vfs_mkdir);
  3034. EXPORT_SYMBOL(vfs_mknod);
  3035. EXPORT_SYMBOL(generic_permission);
  3036. EXPORT_SYMBOL(vfs_readlink);
  3037. EXPORT_SYMBOL(vfs_rename);
  3038. EXPORT_SYMBOL(vfs_rmdir);
  3039. EXPORT_SYMBOL(vfs_symlink);
  3040. EXPORT_SYMBOL(vfs_unlink);
  3041. EXPORT_SYMBOL(dentry_unhash);
  3042. EXPORT_SYMBOL(generic_readlink);