namei.c 70 KB

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