namei.c 71 KB

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