namei.c 70 KB

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