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