namei.c 81 KB

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