namei.c 84 KB

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