namei.c 70 KB

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