namei.c 71 KB

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