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