namei.c 70 KB

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