namei.c 83 KB

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