namespace.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637
  1. /*
  2. * linux/fs/namespace.c
  3. *
  4. * (C) Copyright Al Viro 2000, 2001
  5. * Released under GPL v2.
  6. *
  7. * Based on code from fs/super.c, copyright Linus Torvalds and others.
  8. * Heavily rewritten.
  9. */
  10. #include <linux/syscalls.h>
  11. #include <linux/export.h>
  12. #include <linux/capability.h>
  13. #include <linux/mnt_namespace.h>
  14. #include <linux/namei.h>
  15. #include <linux/security.h>
  16. #include <linux/idr.h>
  17. #include <linux/acct.h> /* acct_auto_close_mnt */
  18. #include <linux/ramfs.h> /* init_rootfs */
  19. #include <linux/fs_struct.h> /* get_fs_root et.al. */
  20. #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
  21. #include <linux/uaccess.h>
  22. #include "pnode.h"
  23. #include "internal.h"
  24. #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
  25. #define HASH_SIZE (1UL << HASH_SHIFT)
  26. static int event;
  27. static DEFINE_IDA(mnt_id_ida);
  28. static DEFINE_IDA(mnt_group_ida);
  29. static DEFINE_SPINLOCK(mnt_id_lock);
  30. static int mnt_id_start = 0;
  31. static int mnt_group_start = 1;
  32. static struct list_head *mount_hashtable __read_mostly;
  33. static struct kmem_cache *mnt_cache __read_mostly;
  34. static struct rw_semaphore namespace_sem;
  35. /* /sys/fs */
  36. struct kobject *fs_kobj;
  37. EXPORT_SYMBOL_GPL(fs_kobj);
  38. /*
  39. * vfsmount lock may be taken for read to prevent changes to the
  40. * vfsmount hash, ie. during mountpoint lookups or walking back
  41. * up the tree.
  42. *
  43. * It should be taken for write in all cases where the vfsmount
  44. * tree or hash is modified or when a vfsmount structure is modified.
  45. */
  46. DEFINE_BRLOCK(vfsmount_lock);
  47. static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
  48. {
  49. unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
  50. tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
  51. tmp = tmp + (tmp >> HASH_SHIFT);
  52. return tmp & (HASH_SIZE - 1);
  53. }
  54. #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
  55. /*
  56. * allocation is serialized by namespace_sem, but we need the spinlock to
  57. * serialize with freeing.
  58. */
  59. static int mnt_alloc_id(struct mount *mnt)
  60. {
  61. int res;
  62. retry:
  63. ida_pre_get(&mnt_id_ida, GFP_KERNEL);
  64. spin_lock(&mnt_id_lock);
  65. res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
  66. if (!res)
  67. mnt_id_start = mnt->mnt_id + 1;
  68. spin_unlock(&mnt_id_lock);
  69. if (res == -EAGAIN)
  70. goto retry;
  71. return res;
  72. }
  73. static void mnt_free_id(struct mount *mnt)
  74. {
  75. int id = mnt->mnt_id;
  76. spin_lock(&mnt_id_lock);
  77. ida_remove(&mnt_id_ida, id);
  78. if (mnt_id_start > id)
  79. mnt_id_start = id;
  80. spin_unlock(&mnt_id_lock);
  81. }
  82. /*
  83. * Allocate a new peer group ID
  84. *
  85. * mnt_group_ida is protected by namespace_sem
  86. */
  87. static int mnt_alloc_group_id(struct mount *mnt)
  88. {
  89. int res;
  90. if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
  91. return -ENOMEM;
  92. res = ida_get_new_above(&mnt_group_ida,
  93. mnt_group_start,
  94. &mnt->mnt_group_id);
  95. if (!res)
  96. mnt_group_start = mnt->mnt_group_id + 1;
  97. return res;
  98. }
  99. /*
  100. * Release a peer group ID
  101. */
  102. void mnt_release_group_id(struct mount *mnt)
  103. {
  104. int id = mnt->mnt_group_id;
  105. ida_remove(&mnt_group_ida, id);
  106. if (mnt_group_start > id)
  107. mnt_group_start = id;
  108. mnt->mnt_group_id = 0;
  109. }
  110. /*
  111. * vfsmount lock must be held for read
  112. */
  113. static inline void mnt_add_count(struct mount *mnt, int n)
  114. {
  115. #ifdef CONFIG_SMP
  116. this_cpu_add(mnt->mnt_pcp->mnt_count, n);
  117. #else
  118. preempt_disable();
  119. mnt->mnt_count += n;
  120. preempt_enable();
  121. #endif
  122. }
  123. /*
  124. * vfsmount lock must be held for write
  125. */
  126. unsigned int mnt_get_count(struct mount *mnt)
  127. {
  128. #ifdef CONFIG_SMP
  129. unsigned int count = 0;
  130. int cpu;
  131. for_each_possible_cpu(cpu) {
  132. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
  133. }
  134. return count;
  135. #else
  136. return mnt->mnt_count;
  137. #endif
  138. }
  139. static struct mount *alloc_vfsmnt(const char *name)
  140. {
  141. struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
  142. if (mnt) {
  143. int err;
  144. err = mnt_alloc_id(mnt);
  145. if (err)
  146. goto out_free_cache;
  147. if (name) {
  148. mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
  149. if (!mnt->mnt_devname)
  150. goto out_free_id;
  151. }
  152. #ifdef CONFIG_SMP
  153. mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
  154. if (!mnt->mnt_pcp)
  155. goto out_free_devname;
  156. this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
  157. #else
  158. mnt->mnt_count = 1;
  159. mnt->mnt_writers = 0;
  160. #endif
  161. INIT_LIST_HEAD(&mnt->mnt_hash);
  162. INIT_LIST_HEAD(&mnt->mnt_child);
  163. INIT_LIST_HEAD(&mnt->mnt_mounts);
  164. INIT_LIST_HEAD(&mnt->mnt_list);
  165. INIT_LIST_HEAD(&mnt->mnt_expire);
  166. INIT_LIST_HEAD(&mnt->mnt_share);
  167. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  168. INIT_LIST_HEAD(&mnt->mnt_slave);
  169. #ifdef CONFIG_FSNOTIFY
  170. INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
  171. #endif
  172. }
  173. return mnt;
  174. #ifdef CONFIG_SMP
  175. out_free_devname:
  176. kfree(mnt->mnt_devname);
  177. #endif
  178. out_free_id:
  179. mnt_free_id(mnt);
  180. out_free_cache:
  181. kmem_cache_free(mnt_cache, mnt);
  182. return NULL;
  183. }
  184. /*
  185. * Most r/o checks on a fs are for operations that take
  186. * discrete amounts of time, like a write() or unlink().
  187. * We must keep track of when those operations start
  188. * (for permission checks) and when they end, so that
  189. * we can determine when writes are able to occur to
  190. * a filesystem.
  191. */
  192. /*
  193. * __mnt_is_readonly: check whether a mount is read-only
  194. * @mnt: the mount to check for its write status
  195. *
  196. * This shouldn't be used directly ouside of the VFS.
  197. * It does not guarantee that the filesystem will stay
  198. * r/w, just that it is right *now*. This can not and
  199. * should not be used in place of IS_RDONLY(inode).
  200. * mnt_want/drop_write() will _keep_ the filesystem
  201. * r/w.
  202. */
  203. int __mnt_is_readonly(struct vfsmount *mnt)
  204. {
  205. if (mnt->mnt_flags & MNT_READONLY)
  206. return 1;
  207. if (mnt->mnt_sb->s_flags & MS_RDONLY)
  208. return 1;
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(__mnt_is_readonly);
  212. static inline void mnt_inc_writers(struct mount *mnt)
  213. {
  214. #ifdef CONFIG_SMP
  215. this_cpu_inc(mnt->mnt_pcp->mnt_writers);
  216. #else
  217. mnt->mnt_writers++;
  218. #endif
  219. }
  220. static inline void mnt_dec_writers(struct mount *mnt)
  221. {
  222. #ifdef CONFIG_SMP
  223. this_cpu_dec(mnt->mnt_pcp->mnt_writers);
  224. #else
  225. mnt->mnt_writers--;
  226. #endif
  227. }
  228. static unsigned int mnt_get_writers(struct mount *mnt)
  229. {
  230. #ifdef CONFIG_SMP
  231. unsigned int count = 0;
  232. int cpu;
  233. for_each_possible_cpu(cpu) {
  234. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
  235. }
  236. return count;
  237. #else
  238. return mnt->mnt_writers;
  239. #endif
  240. }
  241. static int mnt_is_readonly(struct vfsmount *mnt)
  242. {
  243. if (mnt->mnt_sb->s_readonly_remount)
  244. return 1;
  245. /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
  246. smp_rmb();
  247. return __mnt_is_readonly(mnt);
  248. }
  249. /*
  250. * Most r/o checks on a fs are for operations that take
  251. * discrete amounts of time, like a write() or unlink().
  252. * We must keep track of when those operations start
  253. * (for permission checks) and when they end, so that
  254. * we can determine when writes are able to occur to
  255. * a filesystem.
  256. */
  257. /**
  258. * mnt_want_write - get write access to a mount
  259. * @m: the mount on which to take a write
  260. *
  261. * This tells the low-level filesystem that a write is
  262. * about to be performed to it, and makes sure that
  263. * writes are allowed before returning success. When
  264. * the write operation is finished, mnt_drop_write()
  265. * must be called. This is effectively a refcount.
  266. */
  267. int mnt_want_write(struct vfsmount *m)
  268. {
  269. struct mount *mnt = real_mount(m);
  270. int ret = 0;
  271. preempt_disable();
  272. mnt_inc_writers(mnt);
  273. /*
  274. * The store to mnt_inc_writers must be visible before we pass
  275. * MNT_WRITE_HOLD loop below, so that the slowpath can see our
  276. * incremented count after it has set MNT_WRITE_HOLD.
  277. */
  278. smp_mb();
  279. while (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
  280. cpu_relax();
  281. /*
  282. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  283. * be set to match its requirements. So we must not load that until
  284. * MNT_WRITE_HOLD is cleared.
  285. */
  286. smp_rmb();
  287. if (mnt_is_readonly(m)) {
  288. mnt_dec_writers(mnt);
  289. ret = -EROFS;
  290. }
  291. preempt_enable();
  292. return ret;
  293. }
  294. EXPORT_SYMBOL_GPL(mnt_want_write);
  295. /**
  296. * mnt_clone_write - get write access to a mount
  297. * @mnt: the mount on which to take a write
  298. *
  299. * This is effectively like mnt_want_write, except
  300. * it must only be used to take an extra write reference
  301. * on a mountpoint that we already know has a write reference
  302. * on it. This allows some optimisation.
  303. *
  304. * After finished, mnt_drop_write must be called as usual to
  305. * drop the reference.
  306. */
  307. int mnt_clone_write(struct vfsmount *mnt)
  308. {
  309. /* superblock may be r/o */
  310. if (__mnt_is_readonly(mnt))
  311. return -EROFS;
  312. preempt_disable();
  313. mnt_inc_writers(real_mount(mnt));
  314. preempt_enable();
  315. return 0;
  316. }
  317. EXPORT_SYMBOL_GPL(mnt_clone_write);
  318. /**
  319. * mnt_want_write_file - get write access to a file's mount
  320. * @file: the file who's mount on which to take a write
  321. *
  322. * This is like mnt_want_write, but it takes a file and can
  323. * do some optimisations if the file is open for write already
  324. */
  325. int mnt_want_write_file(struct file *file)
  326. {
  327. struct inode *inode = file->f_dentry->d_inode;
  328. if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
  329. return mnt_want_write(file->f_path.mnt);
  330. else
  331. return mnt_clone_write(file->f_path.mnt);
  332. }
  333. EXPORT_SYMBOL_GPL(mnt_want_write_file);
  334. /**
  335. * mnt_drop_write - give up write access to a mount
  336. * @mnt: the mount on which to give up write access
  337. *
  338. * Tells the low-level filesystem that we are done
  339. * performing writes to it. Must be matched with
  340. * mnt_want_write() call above.
  341. */
  342. void mnt_drop_write(struct vfsmount *mnt)
  343. {
  344. preempt_disable();
  345. mnt_dec_writers(real_mount(mnt));
  346. preempt_enable();
  347. }
  348. EXPORT_SYMBOL_GPL(mnt_drop_write);
  349. void mnt_drop_write_file(struct file *file)
  350. {
  351. mnt_drop_write(file->f_path.mnt);
  352. }
  353. EXPORT_SYMBOL(mnt_drop_write_file);
  354. static int mnt_make_readonly(struct mount *mnt)
  355. {
  356. int ret = 0;
  357. br_write_lock(&vfsmount_lock);
  358. mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
  359. /*
  360. * After storing MNT_WRITE_HOLD, we'll read the counters. This store
  361. * should be visible before we do.
  362. */
  363. smp_mb();
  364. /*
  365. * With writers on hold, if this value is zero, then there are
  366. * definitely no active writers (although held writers may subsequently
  367. * increment the count, they'll have to wait, and decrement it after
  368. * seeing MNT_READONLY).
  369. *
  370. * It is OK to have counter incremented on one CPU and decremented on
  371. * another: the sum will add up correctly. The danger would be when we
  372. * sum up each counter, if we read a counter before it is incremented,
  373. * but then read another CPU's count which it has been subsequently
  374. * decremented from -- we would see more decrements than we should.
  375. * MNT_WRITE_HOLD protects against this scenario, because
  376. * mnt_want_write first increments count, then smp_mb, then spins on
  377. * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
  378. * we're counting up here.
  379. */
  380. if (mnt_get_writers(mnt) > 0)
  381. ret = -EBUSY;
  382. else
  383. mnt->mnt.mnt_flags |= MNT_READONLY;
  384. /*
  385. * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
  386. * that become unheld will see MNT_READONLY.
  387. */
  388. smp_wmb();
  389. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  390. br_write_unlock(&vfsmount_lock);
  391. return ret;
  392. }
  393. static void __mnt_unmake_readonly(struct mount *mnt)
  394. {
  395. br_write_lock(&vfsmount_lock);
  396. mnt->mnt.mnt_flags &= ~MNT_READONLY;
  397. br_write_unlock(&vfsmount_lock);
  398. }
  399. int sb_prepare_remount_readonly(struct super_block *sb)
  400. {
  401. struct mount *mnt;
  402. int err = 0;
  403. /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
  404. if (atomic_long_read(&sb->s_remove_count))
  405. return -EBUSY;
  406. br_write_lock(&vfsmount_lock);
  407. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  408. if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
  409. mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
  410. smp_mb();
  411. if (mnt_get_writers(mnt) > 0) {
  412. err = -EBUSY;
  413. break;
  414. }
  415. }
  416. }
  417. if (!err && atomic_long_read(&sb->s_remove_count))
  418. err = -EBUSY;
  419. if (!err) {
  420. sb->s_readonly_remount = 1;
  421. smp_wmb();
  422. }
  423. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  424. if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
  425. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  426. }
  427. br_write_unlock(&vfsmount_lock);
  428. return err;
  429. }
  430. static void free_vfsmnt(struct mount *mnt)
  431. {
  432. kfree(mnt->mnt_devname);
  433. mnt_free_id(mnt);
  434. #ifdef CONFIG_SMP
  435. free_percpu(mnt->mnt_pcp);
  436. #endif
  437. kmem_cache_free(mnt_cache, mnt);
  438. }
  439. /*
  440. * find the first or last mount at @dentry on vfsmount @mnt depending on
  441. * @dir. If @dir is set return the first mount else return the last mount.
  442. * vfsmount_lock must be held for read or write.
  443. */
  444. struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
  445. int dir)
  446. {
  447. struct list_head *head = mount_hashtable + hash(mnt, dentry);
  448. struct list_head *tmp = head;
  449. struct mount *p, *found = NULL;
  450. for (;;) {
  451. tmp = dir ? tmp->next : tmp->prev;
  452. p = NULL;
  453. if (tmp == head)
  454. break;
  455. p = list_entry(tmp, struct mount, mnt_hash);
  456. if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) {
  457. found = p;
  458. break;
  459. }
  460. }
  461. return found;
  462. }
  463. /*
  464. * lookup_mnt increments the ref count before returning
  465. * the vfsmount struct.
  466. */
  467. struct vfsmount *lookup_mnt(struct path *path)
  468. {
  469. struct mount *child_mnt;
  470. br_read_lock(&vfsmount_lock);
  471. child_mnt = __lookup_mnt(path->mnt, path->dentry, 1);
  472. if (child_mnt) {
  473. mnt_add_count(child_mnt, 1);
  474. br_read_unlock(&vfsmount_lock);
  475. return &child_mnt->mnt;
  476. } else {
  477. br_read_unlock(&vfsmount_lock);
  478. return NULL;
  479. }
  480. }
  481. static inline int check_mnt(struct mount *mnt)
  482. {
  483. return mnt->mnt_ns == current->nsproxy->mnt_ns;
  484. }
  485. /*
  486. * vfsmount lock must be held for write
  487. */
  488. static void touch_mnt_namespace(struct mnt_namespace *ns)
  489. {
  490. if (ns) {
  491. ns->event = ++event;
  492. wake_up_interruptible(&ns->poll);
  493. }
  494. }
  495. /*
  496. * vfsmount lock must be held for write
  497. */
  498. static void __touch_mnt_namespace(struct mnt_namespace *ns)
  499. {
  500. if (ns && ns->event != event) {
  501. ns->event = event;
  502. wake_up_interruptible(&ns->poll);
  503. }
  504. }
  505. /*
  506. * Clear dentry's mounted state if it has no remaining mounts.
  507. * vfsmount_lock must be held for write.
  508. */
  509. static void dentry_reset_mounted(struct dentry *dentry)
  510. {
  511. unsigned u;
  512. for (u = 0; u < HASH_SIZE; u++) {
  513. struct mount *p;
  514. list_for_each_entry(p, &mount_hashtable[u], mnt_hash) {
  515. if (p->mnt_mountpoint == dentry)
  516. return;
  517. }
  518. }
  519. spin_lock(&dentry->d_lock);
  520. dentry->d_flags &= ~DCACHE_MOUNTED;
  521. spin_unlock(&dentry->d_lock);
  522. }
  523. /*
  524. * vfsmount lock must be held for write
  525. */
  526. static void detach_mnt(struct mount *mnt, struct path *old_path)
  527. {
  528. old_path->dentry = mnt->mnt_mountpoint;
  529. old_path->mnt = &mnt->mnt_parent->mnt;
  530. mnt->mnt_parent = mnt;
  531. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  532. list_del_init(&mnt->mnt_child);
  533. list_del_init(&mnt->mnt_hash);
  534. dentry_reset_mounted(old_path->dentry);
  535. }
  536. /*
  537. * vfsmount lock must be held for write
  538. */
  539. void mnt_set_mountpoint(struct mount *mnt, struct dentry *dentry,
  540. struct mount *child_mnt)
  541. {
  542. mnt_add_count(mnt, 1); /* essentially, that's mntget */
  543. child_mnt->mnt_mountpoint = dget(dentry);
  544. child_mnt->mnt_parent = mnt;
  545. spin_lock(&dentry->d_lock);
  546. dentry->d_flags |= DCACHE_MOUNTED;
  547. spin_unlock(&dentry->d_lock);
  548. }
  549. /*
  550. * vfsmount lock must be held for write
  551. */
  552. static void attach_mnt(struct mount *mnt, struct path *path)
  553. {
  554. mnt_set_mountpoint(real_mount(path->mnt), path->dentry, mnt);
  555. list_add_tail(&mnt->mnt_hash, mount_hashtable +
  556. hash(path->mnt, path->dentry));
  557. list_add_tail(&mnt->mnt_child, &real_mount(path->mnt)->mnt_mounts);
  558. }
  559. static inline void __mnt_make_longterm(struct mount *mnt)
  560. {
  561. #ifdef CONFIG_SMP
  562. atomic_inc(&mnt->mnt_longterm);
  563. #endif
  564. }
  565. /* needs vfsmount lock for write */
  566. static inline void __mnt_make_shortterm(struct mount *mnt)
  567. {
  568. #ifdef CONFIG_SMP
  569. atomic_dec(&mnt->mnt_longterm);
  570. #endif
  571. }
  572. /*
  573. * vfsmount lock must be held for write
  574. */
  575. static void commit_tree(struct mount *mnt)
  576. {
  577. struct mount *parent = mnt->mnt_parent;
  578. struct mount *m;
  579. LIST_HEAD(head);
  580. struct mnt_namespace *n = parent->mnt_ns;
  581. BUG_ON(parent == mnt);
  582. list_add_tail(&head, &mnt->mnt_list);
  583. list_for_each_entry(m, &head, mnt_list) {
  584. m->mnt_ns = n;
  585. __mnt_make_longterm(m);
  586. }
  587. list_splice(&head, n->list.prev);
  588. list_add_tail(&mnt->mnt_hash, mount_hashtable +
  589. hash(&parent->mnt, mnt->mnt_mountpoint));
  590. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  591. touch_mnt_namespace(n);
  592. }
  593. static struct mount *next_mnt(struct mount *p, struct mount *root)
  594. {
  595. struct list_head *next = p->mnt_mounts.next;
  596. if (next == &p->mnt_mounts) {
  597. while (1) {
  598. if (p == root)
  599. return NULL;
  600. next = p->mnt_child.next;
  601. if (next != &p->mnt_parent->mnt_mounts)
  602. break;
  603. p = p->mnt_parent;
  604. }
  605. }
  606. return list_entry(next, struct mount, mnt_child);
  607. }
  608. static struct mount *skip_mnt_tree(struct mount *p)
  609. {
  610. struct list_head *prev = p->mnt_mounts.prev;
  611. while (prev != &p->mnt_mounts) {
  612. p = list_entry(prev, struct mount, mnt_child);
  613. prev = p->mnt_mounts.prev;
  614. }
  615. return p;
  616. }
  617. struct vfsmount *
  618. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  619. {
  620. struct mount *mnt;
  621. struct dentry *root;
  622. if (!type)
  623. return ERR_PTR(-ENODEV);
  624. mnt = alloc_vfsmnt(name);
  625. if (!mnt)
  626. return ERR_PTR(-ENOMEM);
  627. if (flags & MS_KERNMOUNT)
  628. mnt->mnt.mnt_flags = MNT_INTERNAL;
  629. root = mount_fs(type, flags, name, data);
  630. if (IS_ERR(root)) {
  631. free_vfsmnt(mnt);
  632. return ERR_CAST(root);
  633. }
  634. mnt->mnt.mnt_root = root;
  635. mnt->mnt.mnt_sb = root->d_sb;
  636. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  637. mnt->mnt_parent = mnt;
  638. br_write_lock(&vfsmount_lock);
  639. list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
  640. br_write_unlock(&vfsmount_lock);
  641. return &mnt->mnt;
  642. }
  643. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  644. static struct mount *clone_mnt(struct mount *old, struct dentry *root,
  645. int flag)
  646. {
  647. struct super_block *sb = old->mnt.mnt_sb;
  648. struct mount *mnt = alloc_vfsmnt(old->mnt_devname);
  649. if (mnt) {
  650. if (flag & (CL_SLAVE | CL_PRIVATE))
  651. mnt->mnt_group_id = 0; /* not a peer of original */
  652. else
  653. mnt->mnt_group_id = old->mnt_group_id;
  654. if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
  655. int err = mnt_alloc_group_id(mnt);
  656. if (err)
  657. goto out_free;
  658. }
  659. mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD;
  660. atomic_inc(&sb->s_active);
  661. mnt->mnt.mnt_sb = sb;
  662. mnt->mnt.mnt_root = dget(root);
  663. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  664. mnt->mnt_parent = mnt;
  665. br_write_lock(&vfsmount_lock);
  666. list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
  667. br_write_unlock(&vfsmount_lock);
  668. if (flag & CL_SLAVE) {
  669. list_add(&mnt->mnt_slave, &old->mnt_slave_list);
  670. mnt->mnt_master = old;
  671. CLEAR_MNT_SHARED(mnt);
  672. } else if (!(flag & CL_PRIVATE)) {
  673. if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
  674. list_add(&mnt->mnt_share, &old->mnt_share);
  675. if (IS_MNT_SLAVE(old))
  676. list_add(&mnt->mnt_slave, &old->mnt_slave);
  677. mnt->mnt_master = old->mnt_master;
  678. }
  679. if (flag & CL_MAKE_SHARED)
  680. set_mnt_shared(mnt);
  681. /* stick the duplicate mount on the same expiry list
  682. * as the original if that was on one */
  683. if (flag & CL_EXPIRE) {
  684. if (!list_empty(&old->mnt_expire))
  685. list_add(&mnt->mnt_expire, &old->mnt_expire);
  686. }
  687. }
  688. return mnt;
  689. out_free:
  690. free_vfsmnt(mnt);
  691. return NULL;
  692. }
  693. static inline void mntfree(struct mount *mnt)
  694. {
  695. struct vfsmount *m = &mnt->mnt;
  696. struct super_block *sb = m->mnt_sb;
  697. /*
  698. * This probably indicates that somebody messed
  699. * up a mnt_want/drop_write() pair. If this
  700. * happens, the filesystem was probably unable
  701. * to make r/w->r/o transitions.
  702. */
  703. /*
  704. * The locking used to deal with mnt_count decrement provides barriers,
  705. * so mnt_get_writers() below is safe.
  706. */
  707. WARN_ON(mnt_get_writers(mnt));
  708. fsnotify_vfsmount_delete(m);
  709. dput(m->mnt_root);
  710. free_vfsmnt(mnt);
  711. deactivate_super(sb);
  712. }
  713. static void mntput_no_expire(struct mount *mnt)
  714. {
  715. put_again:
  716. #ifdef CONFIG_SMP
  717. br_read_lock(&vfsmount_lock);
  718. if (likely(atomic_read(&mnt->mnt_longterm))) {
  719. mnt_add_count(mnt, -1);
  720. br_read_unlock(&vfsmount_lock);
  721. return;
  722. }
  723. br_read_unlock(&vfsmount_lock);
  724. br_write_lock(&vfsmount_lock);
  725. mnt_add_count(mnt, -1);
  726. if (mnt_get_count(mnt)) {
  727. br_write_unlock(&vfsmount_lock);
  728. return;
  729. }
  730. #else
  731. mnt_add_count(mnt, -1);
  732. if (likely(mnt_get_count(mnt)))
  733. return;
  734. br_write_lock(&vfsmount_lock);
  735. #endif
  736. if (unlikely(mnt->mnt_pinned)) {
  737. mnt_add_count(mnt, mnt->mnt_pinned + 1);
  738. mnt->mnt_pinned = 0;
  739. br_write_unlock(&vfsmount_lock);
  740. acct_auto_close_mnt(&mnt->mnt);
  741. goto put_again;
  742. }
  743. list_del(&mnt->mnt_instance);
  744. br_write_unlock(&vfsmount_lock);
  745. mntfree(mnt);
  746. }
  747. void mntput(struct vfsmount *mnt)
  748. {
  749. if (mnt) {
  750. struct mount *m = real_mount(mnt);
  751. /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
  752. if (unlikely(m->mnt_expiry_mark))
  753. m->mnt_expiry_mark = 0;
  754. mntput_no_expire(m);
  755. }
  756. }
  757. EXPORT_SYMBOL(mntput);
  758. struct vfsmount *mntget(struct vfsmount *mnt)
  759. {
  760. if (mnt)
  761. mnt_add_count(real_mount(mnt), 1);
  762. return mnt;
  763. }
  764. EXPORT_SYMBOL(mntget);
  765. void mnt_pin(struct vfsmount *mnt)
  766. {
  767. br_write_lock(&vfsmount_lock);
  768. real_mount(mnt)->mnt_pinned++;
  769. br_write_unlock(&vfsmount_lock);
  770. }
  771. EXPORT_SYMBOL(mnt_pin);
  772. void mnt_unpin(struct vfsmount *m)
  773. {
  774. struct mount *mnt = real_mount(m);
  775. br_write_lock(&vfsmount_lock);
  776. if (mnt->mnt_pinned) {
  777. mnt_add_count(mnt, 1);
  778. mnt->mnt_pinned--;
  779. }
  780. br_write_unlock(&vfsmount_lock);
  781. }
  782. EXPORT_SYMBOL(mnt_unpin);
  783. static inline void mangle(struct seq_file *m, const char *s)
  784. {
  785. seq_escape(m, s, " \t\n\\");
  786. }
  787. /*
  788. * Simple .show_options callback for filesystems which don't want to
  789. * implement more complex mount option showing.
  790. *
  791. * See also save_mount_options().
  792. */
  793. int generic_show_options(struct seq_file *m, struct dentry *root)
  794. {
  795. const char *options;
  796. rcu_read_lock();
  797. options = rcu_dereference(root->d_sb->s_options);
  798. if (options != NULL && options[0]) {
  799. seq_putc(m, ',');
  800. mangle(m, options);
  801. }
  802. rcu_read_unlock();
  803. return 0;
  804. }
  805. EXPORT_SYMBOL(generic_show_options);
  806. /*
  807. * If filesystem uses generic_show_options(), this function should be
  808. * called from the fill_super() callback.
  809. *
  810. * The .remount_fs callback usually needs to be handled in a special
  811. * way, to make sure, that previous options are not overwritten if the
  812. * remount fails.
  813. *
  814. * Also note, that if the filesystem's .remount_fs function doesn't
  815. * reset all options to their default value, but changes only newly
  816. * given options, then the displayed options will not reflect reality
  817. * any more.
  818. */
  819. void save_mount_options(struct super_block *sb, char *options)
  820. {
  821. BUG_ON(sb->s_options);
  822. rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
  823. }
  824. EXPORT_SYMBOL(save_mount_options);
  825. void replace_mount_options(struct super_block *sb, char *options)
  826. {
  827. char *old = sb->s_options;
  828. rcu_assign_pointer(sb->s_options, options);
  829. if (old) {
  830. synchronize_rcu();
  831. kfree(old);
  832. }
  833. }
  834. EXPORT_SYMBOL(replace_mount_options);
  835. #ifdef CONFIG_PROC_FS
  836. /* iterator; we want it to have access to namespace_sem, thus here... */
  837. static void *m_start(struct seq_file *m, loff_t *pos)
  838. {
  839. struct proc_mounts *p = container_of(m, struct proc_mounts, m);
  840. down_read(&namespace_sem);
  841. return seq_list_start(&p->ns->list, *pos);
  842. }
  843. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  844. {
  845. struct proc_mounts *p = container_of(m, struct proc_mounts, m);
  846. return seq_list_next(v, &p->ns->list, pos);
  847. }
  848. static void m_stop(struct seq_file *m, void *v)
  849. {
  850. up_read(&namespace_sem);
  851. }
  852. static int m_show(struct seq_file *m, void *v)
  853. {
  854. struct proc_mounts *p = container_of(m, struct proc_mounts, m);
  855. struct mount *r = list_entry(v, struct mount, mnt_list);
  856. return p->show(m, &r->mnt);
  857. }
  858. const struct seq_operations mounts_op = {
  859. .start = m_start,
  860. .next = m_next,
  861. .stop = m_stop,
  862. .show = m_show,
  863. };
  864. #endif /* CONFIG_PROC_FS */
  865. /**
  866. * may_umount_tree - check if a mount tree is busy
  867. * @mnt: root of mount tree
  868. *
  869. * This is called to check if a tree of mounts has any
  870. * open files, pwds, chroots or sub mounts that are
  871. * busy.
  872. */
  873. int may_umount_tree(struct vfsmount *m)
  874. {
  875. struct mount *mnt = real_mount(m);
  876. int actual_refs = 0;
  877. int minimum_refs = 0;
  878. struct mount *p;
  879. BUG_ON(!m);
  880. /* write lock needed for mnt_get_count */
  881. br_write_lock(&vfsmount_lock);
  882. for (p = mnt; p; p = next_mnt(p, mnt)) {
  883. actual_refs += mnt_get_count(p);
  884. minimum_refs += 2;
  885. }
  886. br_write_unlock(&vfsmount_lock);
  887. if (actual_refs > minimum_refs)
  888. return 0;
  889. return 1;
  890. }
  891. EXPORT_SYMBOL(may_umount_tree);
  892. /**
  893. * may_umount - check if a mount point is busy
  894. * @mnt: root of mount
  895. *
  896. * This is called to check if a mount point has any
  897. * open files, pwds, chroots or sub mounts. If the
  898. * mount has sub mounts this will return busy
  899. * regardless of whether the sub mounts are busy.
  900. *
  901. * Doesn't take quota and stuff into account. IOW, in some cases it will
  902. * give false negatives. The main reason why it's here is that we need
  903. * a non-destructive way to look for easily umountable filesystems.
  904. */
  905. int may_umount(struct vfsmount *mnt)
  906. {
  907. int ret = 1;
  908. down_read(&namespace_sem);
  909. br_write_lock(&vfsmount_lock);
  910. if (propagate_mount_busy(real_mount(mnt), 2))
  911. ret = 0;
  912. br_write_unlock(&vfsmount_lock);
  913. up_read(&namespace_sem);
  914. return ret;
  915. }
  916. EXPORT_SYMBOL(may_umount);
  917. void release_mounts(struct list_head *head)
  918. {
  919. struct mount *mnt;
  920. while (!list_empty(head)) {
  921. mnt = list_first_entry(head, struct mount, mnt_hash);
  922. list_del_init(&mnt->mnt_hash);
  923. if (mnt_has_parent(mnt)) {
  924. struct dentry *dentry;
  925. struct mount *m;
  926. br_write_lock(&vfsmount_lock);
  927. dentry = mnt->mnt_mountpoint;
  928. m = mnt->mnt_parent;
  929. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  930. mnt->mnt_parent = mnt;
  931. m->mnt_ghosts--;
  932. br_write_unlock(&vfsmount_lock);
  933. dput(dentry);
  934. mntput(&m->mnt);
  935. }
  936. mntput(&mnt->mnt);
  937. }
  938. }
  939. /*
  940. * vfsmount lock must be held for write
  941. * namespace_sem must be held for write
  942. */
  943. void umount_tree(struct mount *mnt, int propagate, struct list_head *kill)
  944. {
  945. LIST_HEAD(tmp_list);
  946. struct mount *p;
  947. for (p = mnt; p; p = next_mnt(p, mnt))
  948. list_move(&p->mnt_hash, &tmp_list);
  949. if (propagate)
  950. propagate_umount(&tmp_list);
  951. list_for_each_entry(p, &tmp_list, mnt_hash) {
  952. list_del_init(&p->mnt_expire);
  953. list_del_init(&p->mnt_list);
  954. __touch_mnt_namespace(p->mnt_ns);
  955. if (p->mnt_ns)
  956. __mnt_make_shortterm(p);
  957. p->mnt_ns = NULL;
  958. list_del_init(&p->mnt_child);
  959. if (mnt_has_parent(p)) {
  960. p->mnt_parent->mnt_ghosts++;
  961. dentry_reset_mounted(p->mnt_mountpoint);
  962. }
  963. change_mnt_propagation(p, MS_PRIVATE);
  964. }
  965. list_splice(&tmp_list, kill);
  966. }
  967. static void shrink_submounts(struct mount *mnt, struct list_head *umounts);
  968. static int do_umount(struct mount *mnt, int flags)
  969. {
  970. struct super_block *sb = mnt->mnt.mnt_sb;
  971. int retval;
  972. LIST_HEAD(umount_list);
  973. retval = security_sb_umount(&mnt->mnt, flags);
  974. if (retval)
  975. return retval;
  976. /*
  977. * Allow userspace to request a mountpoint be expired rather than
  978. * unmounting unconditionally. Unmount only happens if:
  979. * (1) the mark is already set (the mark is cleared by mntput())
  980. * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
  981. */
  982. if (flags & MNT_EXPIRE) {
  983. if (&mnt->mnt == current->fs->root.mnt ||
  984. flags & (MNT_FORCE | MNT_DETACH))
  985. return -EINVAL;
  986. /*
  987. * probably don't strictly need the lock here if we examined
  988. * all race cases, but it's a slowpath.
  989. */
  990. br_write_lock(&vfsmount_lock);
  991. if (mnt_get_count(mnt) != 2) {
  992. br_write_unlock(&vfsmount_lock);
  993. return -EBUSY;
  994. }
  995. br_write_unlock(&vfsmount_lock);
  996. if (!xchg(&mnt->mnt_expiry_mark, 1))
  997. return -EAGAIN;
  998. }
  999. /*
  1000. * If we may have to abort operations to get out of this
  1001. * mount, and they will themselves hold resources we must
  1002. * allow the fs to do things. In the Unix tradition of
  1003. * 'Gee thats tricky lets do it in userspace' the umount_begin
  1004. * might fail to complete on the first run through as other tasks
  1005. * must return, and the like. Thats for the mount program to worry
  1006. * about for the moment.
  1007. */
  1008. if (flags & MNT_FORCE && sb->s_op->umount_begin) {
  1009. sb->s_op->umount_begin(sb);
  1010. }
  1011. /*
  1012. * No sense to grab the lock for this test, but test itself looks
  1013. * somewhat bogus. Suggestions for better replacement?
  1014. * Ho-hum... In principle, we might treat that as umount + switch
  1015. * to rootfs. GC would eventually take care of the old vfsmount.
  1016. * Actually it makes sense, especially if rootfs would contain a
  1017. * /reboot - static binary that would close all descriptors and
  1018. * call reboot(9). Then init(8) could umount root and exec /reboot.
  1019. */
  1020. if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
  1021. /*
  1022. * Special case for "unmounting" root ...
  1023. * we just try to remount it readonly.
  1024. */
  1025. down_write(&sb->s_umount);
  1026. if (!(sb->s_flags & MS_RDONLY))
  1027. retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
  1028. up_write(&sb->s_umount);
  1029. return retval;
  1030. }
  1031. down_write(&namespace_sem);
  1032. br_write_lock(&vfsmount_lock);
  1033. event++;
  1034. if (!(flags & MNT_DETACH))
  1035. shrink_submounts(mnt, &umount_list);
  1036. retval = -EBUSY;
  1037. if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
  1038. if (!list_empty(&mnt->mnt_list))
  1039. umount_tree(mnt, 1, &umount_list);
  1040. retval = 0;
  1041. }
  1042. br_write_unlock(&vfsmount_lock);
  1043. up_write(&namespace_sem);
  1044. release_mounts(&umount_list);
  1045. return retval;
  1046. }
  1047. /*
  1048. * Now umount can handle mount points as well as block devices.
  1049. * This is important for filesystems which use unnamed block devices.
  1050. *
  1051. * We now support a flag for forced unmount like the other 'big iron'
  1052. * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
  1053. */
  1054. SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
  1055. {
  1056. struct path path;
  1057. struct mount *mnt;
  1058. int retval;
  1059. int lookup_flags = 0;
  1060. if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
  1061. return -EINVAL;
  1062. if (!(flags & UMOUNT_NOFOLLOW))
  1063. lookup_flags |= LOOKUP_FOLLOW;
  1064. retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
  1065. if (retval)
  1066. goto out;
  1067. mnt = real_mount(path.mnt);
  1068. retval = -EINVAL;
  1069. if (path.dentry != path.mnt->mnt_root)
  1070. goto dput_and_out;
  1071. if (!check_mnt(mnt))
  1072. goto dput_and_out;
  1073. retval = -EPERM;
  1074. if (!capable(CAP_SYS_ADMIN))
  1075. goto dput_and_out;
  1076. retval = do_umount(mnt, flags);
  1077. dput_and_out:
  1078. /* we mustn't call path_put() as that would clear mnt_expiry_mark */
  1079. dput(path.dentry);
  1080. mntput_no_expire(mnt);
  1081. out:
  1082. return retval;
  1083. }
  1084. #ifdef __ARCH_WANT_SYS_OLDUMOUNT
  1085. /*
  1086. * The 2.0 compatible umount. No flags.
  1087. */
  1088. SYSCALL_DEFINE1(oldumount, char __user *, name)
  1089. {
  1090. return sys_umount(name, 0);
  1091. }
  1092. #endif
  1093. static int mount_is_safe(struct path *path)
  1094. {
  1095. if (capable(CAP_SYS_ADMIN))
  1096. return 0;
  1097. return -EPERM;
  1098. #ifdef notyet
  1099. if (S_ISLNK(path->dentry->d_inode->i_mode))
  1100. return -EPERM;
  1101. if (path->dentry->d_inode->i_mode & S_ISVTX) {
  1102. if (current_uid() != path->dentry->d_inode->i_uid)
  1103. return -EPERM;
  1104. }
  1105. if (inode_permission(path->dentry->d_inode, MAY_WRITE))
  1106. return -EPERM;
  1107. return 0;
  1108. #endif
  1109. }
  1110. struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
  1111. int flag)
  1112. {
  1113. struct mount *res, *p, *q, *r;
  1114. struct path path;
  1115. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
  1116. return NULL;
  1117. res = q = clone_mnt(mnt, dentry, flag);
  1118. if (!q)
  1119. goto Enomem;
  1120. q->mnt_mountpoint = mnt->mnt_mountpoint;
  1121. p = mnt;
  1122. list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
  1123. struct mount *s;
  1124. if (!is_subdir(r->mnt_mountpoint, dentry))
  1125. continue;
  1126. for (s = r; s; s = next_mnt(s, r)) {
  1127. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
  1128. s = skip_mnt_tree(s);
  1129. continue;
  1130. }
  1131. while (p != s->mnt_parent) {
  1132. p = p->mnt_parent;
  1133. q = q->mnt_parent;
  1134. }
  1135. p = s;
  1136. path.mnt = &q->mnt;
  1137. path.dentry = p->mnt_mountpoint;
  1138. q = clone_mnt(p, p->mnt.mnt_root, flag);
  1139. if (!q)
  1140. goto Enomem;
  1141. br_write_lock(&vfsmount_lock);
  1142. list_add_tail(&q->mnt_list, &res->mnt_list);
  1143. attach_mnt(q, &path);
  1144. br_write_unlock(&vfsmount_lock);
  1145. }
  1146. }
  1147. return res;
  1148. Enomem:
  1149. if (res) {
  1150. LIST_HEAD(umount_list);
  1151. br_write_lock(&vfsmount_lock);
  1152. umount_tree(res, 0, &umount_list);
  1153. br_write_unlock(&vfsmount_lock);
  1154. release_mounts(&umount_list);
  1155. }
  1156. return NULL;
  1157. }
  1158. struct vfsmount *collect_mounts(struct path *path)
  1159. {
  1160. struct mount *tree;
  1161. down_write(&namespace_sem);
  1162. tree = copy_tree(real_mount(path->mnt), path->dentry,
  1163. CL_COPY_ALL | CL_PRIVATE);
  1164. up_write(&namespace_sem);
  1165. return tree ? &tree->mnt : NULL;
  1166. }
  1167. void drop_collected_mounts(struct vfsmount *mnt)
  1168. {
  1169. LIST_HEAD(umount_list);
  1170. down_write(&namespace_sem);
  1171. br_write_lock(&vfsmount_lock);
  1172. umount_tree(real_mount(mnt), 0, &umount_list);
  1173. br_write_unlock(&vfsmount_lock);
  1174. up_write(&namespace_sem);
  1175. release_mounts(&umount_list);
  1176. }
  1177. int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
  1178. struct vfsmount *root)
  1179. {
  1180. struct mount *mnt;
  1181. int res = f(root, arg);
  1182. if (res)
  1183. return res;
  1184. list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
  1185. res = f(&mnt->mnt, arg);
  1186. if (res)
  1187. return res;
  1188. }
  1189. return 0;
  1190. }
  1191. static void cleanup_group_ids(struct mount *mnt, struct mount *end)
  1192. {
  1193. struct mount *p;
  1194. for (p = mnt; p != end; p = next_mnt(p, mnt)) {
  1195. if (p->mnt_group_id && !IS_MNT_SHARED(p))
  1196. mnt_release_group_id(p);
  1197. }
  1198. }
  1199. static int invent_group_ids(struct mount *mnt, bool recurse)
  1200. {
  1201. struct mount *p;
  1202. for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
  1203. if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
  1204. int err = mnt_alloc_group_id(p);
  1205. if (err) {
  1206. cleanup_group_ids(mnt, p);
  1207. return err;
  1208. }
  1209. }
  1210. }
  1211. return 0;
  1212. }
  1213. /*
  1214. * @source_mnt : mount tree to be attached
  1215. * @nd : place the mount tree @source_mnt is attached
  1216. * @parent_nd : if non-null, detach the source_mnt from its parent and
  1217. * store the parent mount and mountpoint dentry.
  1218. * (done when source_mnt is moved)
  1219. *
  1220. * NOTE: in the table below explains the semantics when a source mount
  1221. * of a given type is attached to a destination mount of a given type.
  1222. * ---------------------------------------------------------------------------
  1223. * | BIND MOUNT OPERATION |
  1224. * |**************************************************************************
  1225. * | source-->| shared | private | slave | unbindable |
  1226. * | dest | | | | |
  1227. * | | | | | | |
  1228. * | v | | | | |
  1229. * |**************************************************************************
  1230. * | shared | shared (++) | shared (+) | shared(+++)| invalid |
  1231. * | | | | | |
  1232. * |non-shared| shared (+) | private | slave (*) | invalid |
  1233. * ***************************************************************************
  1234. * A bind operation clones the source mount and mounts the clone on the
  1235. * destination mount.
  1236. *
  1237. * (++) the cloned mount is propagated to all the mounts in the propagation
  1238. * tree of the destination mount and the cloned mount is added to
  1239. * the peer group of the source mount.
  1240. * (+) the cloned mount is created under the destination mount and is marked
  1241. * as shared. The cloned mount is added to the peer group of the source
  1242. * mount.
  1243. * (+++) the mount is propagated to all the mounts in the propagation tree
  1244. * of the destination mount and the cloned mount is made slave
  1245. * of the same master as that of the source mount. The cloned mount
  1246. * is marked as 'shared and slave'.
  1247. * (*) the cloned mount is made a slave of the same master as that of the
  1248. * source mount.
  1249. *
  1250. * ---------------------------------------------------------------------------
  1251. * | MOVE MOUNT OPERATION |
  1252. * |**************************************************************************
  1253. * | source-->| shared | private | slave | unbindable |
  1254. * | dest | | | | |
  1255. * | | | | | | |
  1256. * | v | | | | |
  1257. * |**************************************************************************
  1258. * | shared | shared (+) | shared (+) | shared(+++) | invalid |
  1259. * | | | | | |
  1260. * |non-shared| shared (+*) | private | slave (*) | unbindable |
  1261. * ***************************************************************************
  1262. *
  1263. * (+) the mount is moved to the destination. And is then propagated to
  1264. * all the mounts in the propagation tree of the destination mount.
  1265. * (+*) the mount is moved to the destination.
  1266. * (+++) the mount is moved to the destination and is then propagated to
  1267. * all the mounts belonging to the destination mount's propagation tree.
  1268. * the mount is marked as 'shared and slave'.
  1269. * (*) the mount continues to be a slave at the new location.
  1270. *
  1271. * if the source mount is a tree, the operations explained above is
  1272. * applied to each mount in the tree.
  1273. * Must be called without spinlocks held, since this function can sleep
  1274. * in allocations.
  1275. */
  1276. static int attach_recursive_mnt(struct mount *source_mnt,
  1277. struct path *path, struct path *parent_path)
  1278. {
  1279. LIST_HEAD(tree_list);
  1280. struct mount *dest_mnt = real_mount(path->mnt);
  1281. struct dentry *dest_dentry = path->dentry;
  1282. struct mount *child, *p;
  1283. int err;
  1284. if (IS_MNT_SHARED(dest_mnt)) {
  1285. err = invent_group_ids(source_mnt, true);
  1286. if (err)
  1287. goto out;
  1288. }
  1289. err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
  1290. if (err)
  1291. goto out_cleanup_ids;
  1292. br_write_lock(&vfsmount_lock);
  1293. if (IS_MNT_SHARED(dest_mnt)) {
  1294. for (p = source_mnt; p; p = next_mnt(p, source_mnt))
  1295. set_mnt_shared(p);
  1296. }
  1297. if (parent_path) {
  1298. detach_mnt(source_mnt, parent_path);
  1299. attach_mnt(source_mnt, path);
  1300. touch_mnt_namespace(source_mnt->mnt_ns);
  1301. } else {
  1302. mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
  1303. commit_tree(source_mnt);
  1304. }
  1305. list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
  1306. list_del_init(&child->mnt_hash);
  1307. commit_tree(child);
  1308. }
  1309. br_write_unlock(&vfsmount_lock);
  1310. return 0;
  1311. out_cleanup_ids:
  1312. if (IS_MNT_SHARED(dest_mnt))
  1313. cleanup_group_ids(source_mnt, NULL);
  1314. out:
  1315. return err;
  1316. }
  1317. static int lock_mount(struct path *path)
  1318. {
  1319. struct vfsmount *mnt;
  1320. retry:
  1321. mutex_lock(&path->dentry->d_inode->i_mutex);
  1322. if (unlikely(cant_mount(path->dentry))) {
  1323. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1324. return -ENOENT;
  1325. }
  1326. down_write(&namespace_sem);
  1327. mnt = lookup_mnt(path);
  1328. if (likely(!mnt))
  1329. return 0;
  1330. up_write(&namespace_sem);
  1331. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1332. path_put(path);
  1333. path->mnt = mnt;
  1334. path->dentry = dget(mnt->mnt_root);
  1335. goto retry;
  1336. }
  1337. static void unlock_mount(struct path *path)
  1338. {
  1339. up_write(&namespace_sem);
  1340. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1341. }
  1342. static int graft_tree(struct mount *mnt, struct path *path)
  1343. {
  1344. if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
  1345. return -EINVAL;
  1346. if (S_ISDIR(path->dentry->d_inode->i_mode) !=
  1347. S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
  1348. return -ENOTDIR;
  1349. if (d_unlinked(path->dentry))
  1350. return -ENOENT;
  1351. return attach_recursive_mnt(mnt, path, NULL);
  1352. }
  1353. /*
  1354. * Sanity check the flags to change_mnt_propagation.
  1355. */
  1356. static int flags_to_propagation_type(int flags)
  1357. {
  1358. int type = flags & ~(MS_REC | MS_SILENT);
  1359. /* Fail if any non-propagation flags are set */
  1360. if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1361. return 0;
  1362. /* Only one propagation flag should be set */
  1363. if (!is_power_of_2(type))
  1364. return 0;
  1365. return type;
  1366. }
  1367. /*
  1368. * recursively change the type of the mountpoint.
  1369. */
  1370. static int do_change_type(struct path *path, int flag)
  1371. {
  1372. struct mount *m;
  1373. struct mount *mnt = real_mount(path->mnt);
  1374. int recurse = flag & MS_REC;
  1375. int type;
  1376. int err = 0;
  1377. if (!capable(CAP_SYS_ADMIN))
  1378. return -EPERM;
  1379. if (path->dentry != path->mnt->mnt_root)
  1380. return -EINVAL;
  1381. type = flags_to_propagation_type(flag);
  1382. if (!type)
  1383. return -EINVAL;
  1384. down_write(&namespace_sem);
  1385. if (type == MS_SHARED) {
  1386. err = invent_group_ids(mnt, recurse);
  1387. if (err)
  1388. goto out_unlock;
  1389. }
  1390. br_write_lock(&vfsmount_lock);
  1391. for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
  1392. change_mnt_propagation(m, type);
  1393. br_write_unlock(&vfsmount_lock);
  1394. out_unlock:
  1395. up_write(&namespace_sem);
  1396. return err;
  1397. }
  1398. /*
  1399. * do loopback mount.
  1400. */
  1401. static int do_loopback(struct path *path, char *old_name,
  1402. int recurse)
  1403. {
  1404. LIST_HEAD(umount_list);
  1405. struct path old_path;
  1406. struct mount *mnt = NULL, *old;
  1407. int err = mount_is_safe(path);
  1408. if (err)
  1409. return err;
  1410. if (!old_name || !*old_name)
  1411. return -EINVAL;
  1412. err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
  1413. if (err)
  1414. return err;
  1415. err = lock_mount(path);
  1416. if (err)
  1417. goto out;
  1418. old = real_mount(old_path.mnt);
  1419. err = -EINVAL;
  1420. if (IS_MNT_UNBINDABLE(old))
  1421. goto out2;
  1422. if (!check_mnt(real_mount(path->mnt)) || !check_mnt(old))
  1423. goto out2;
  1424. err = -ENOMEM;
  1425. if (recurse)
  1426. mnt = copy_tree(old, old_path.dentry, 0);
  1427. else
  1428. mnt = clone_mnt(old, old_path.dentry, 0);
  1429. if (!mnt)
  1430. goto out2;
  1431. err = graft_tree(mnt, path);
  1432. if (err) {
  1433. br_write_lock(&vfsmount_lock);
  1434. umount_tree(mnt, 0, &umount_list);
  1435. br_write_unlock(&vfsmount_lock);
  1436. }
  1437. out2:
  1438. unlock_mount(path);
  1439. release_mounts(&umount_list);
  1440. out:
  1441. path_put(&old_path);
  1442. return err;
  1443. }
  1444. static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
  1445. {
  1446. int error = 0;
  1447. int readonly_request = 0;
  1448. if (ms_flags & MS_RDONLY)
  1449. readonly_request = 1;
  1450. if (readonly_request == __mnt_is_readonly(mnt))
  1451. return 0;
  1452. if (readonly_request)
  1453. error = mnt_make_readonly(real_mount(mnt));
  1454. else
  1455. __mnt_unmake_readonly(real_mount(mnt));
  1456. return error;
  1457. }
  1458. /*
  1459. * change filesystem flags. dir should be a physical root of filesystem.
  1460. * If you've mounted a non-root directory somewhere and want to do remount
  1461. * on it - tough luck.
  1462. */
  1463. static int do_remount(struct path *path, int flags, int mnt_flags,
  1464. void *data)
  1465. {
  1466. int err;
  1467. struct super_block *sb = path->mnt->mnt_sb;
  1468. struct mount *mnt = real_mount(path->mnt);
  1469. if (!capable(CAP_SYS_ADMIN))
  1470. return -EPERM;
  1471. if (!check_mnt(mnt))
  1472. return -EINVAL;
  1473. if (path->dentry != path->mnt->mnt_root)
  1474. return -EINVAL;
  1475. err = security_sb_remount(sb, data);
  1476. if (err)
  1477. return err;
  1478. down_write(&sb->s_umount);
  1479. if (flags & MS_BIND)
  1480. err = change_mount_flags(path->mnt, flags);
  1481. else
  1482. err = do_remount_sb(sb, flags, data, 0);
  1483. if (!err) {
  1484. br_write_lock(&vfsmount_lock);
  1485. mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK;
  1486. mnt->mnt.mnt_flags = mnt_flags;
  1487. br_write_unlock(&vfsmount_lock);
  1488. }
  1489. up_write(&sb->s_umount);
  1490. if (!err) {
  1491. br_write_lock(&vfsmount_lock);
  1492. touch_mnt_namespace(mnt->mnt_ns);
  1493. br_write_unlock(&vfsmount_lock);
  1494. }
  1495. return err;
  1496. }
  1497. static inline int tree_contains_unbindable(struct mount *mnt)
  1498. {
  1499. struct mount *p;
  1500. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1501. if (IS_MNT_UNBINDABLE(p))
  1502. return 1;
  1503. }
  1504. return 0;
  1505. }
  1506. static int do_move_mount(struct path *path, char *old_name)
  1507. {
  1508. struct path old_path, parent_path;
  1509. struct mount *p;
  1510. struct mount *old;
  1511. int err = 0;
  1512. if (!capable(CAP_SYS_ADMIN))
  1513. return -EPERM;
  1514. if (!old_name || !*old_name)
  1515. return -EINVAL;
  1516. err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
  1517. if (err)
  1518. return err;
  1519. err = lock_mount(path);
  1520. if (err < 0)
  1521. goto out;
  1522. old = real_mount(old_path.mnt);
  1523. p = real_mount(path->mnt);
  1524. err = -EINVAL;
  1525. if (!check_mnt(p) || !check_mnt(old))
  1526. goto out1;
  1527. if (d_unlinked(path->dentry))
  1528. goto out1;
  1529. err = -EINVAL;
  1530. if (old_path.dentry != old_path.mnt->mnt_root)
  1531. goto out1;
  1532. if (!mnt_has_parent(old))
  1533. goto out1;
  1534. if (S_ISDIR(path->dentry->d_inode->i_mode) !=
  1535. S_ISDIR(old_path.dentry->d_inode->i_mode))
  1536. goto out1;
  1537. /*
  1538. * Don't move a mount residing in a shared parent.
  1539. */
  1540. if (IS_MNT_SHARED(old->mnt_parent))
  1541. goto out1;
  1542. /*
  1543. * Don't move a mount tree containing unbindable mounts to a destination
  1544. * mount which is shared.
  1545. */
  1546. if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
  1547. goto out1;
  1548. err = -ELOOP;
  1549. for (; mnt_has_parent(p); p = p->mnt_parent)
  1550. if (p == old)
  1551. goto out1;
  1552. err = attach_recursive_mnt(old, path, &parent_path);
  1553. if (err)
  1554. goto out1;
  1555. /* if the mount is moved, it should no longer be expire
  1556. * automatically */
  1557. list_del_init(&old->mnt_expire);
  1558. out1:
  1559. unlock_mount(path);
  1560. out:
  1561. if (!err)
  1562. path_put(&parent_path);
  1563. path_put(&old_path);
  1564. return err;
  1565. }
  1566. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  1567. {
  1568. int err;
  1569. const char *subtype = strchr(fstype, '.');
  1570. if (subtype) {
  1571. subtype++;
  1572. err = -EINVAL;
  1573. if (!subtype[0])
  1574. goto err;
  1575. } else
  1576. subtype = "";
  1577. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  1578. err = -ENOMEM;
  1579. if (!mnt->mnt_sb->s_subtype)
  1580. goto err;
  1581. return mnt;
  1582. err:
  1583. mntput(mnt);
  1584. return ERR_PTR(err);
  1585. }
  1586. static struct vfsmount *
  1587. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  1588. {
  1589. struct file_system_type *type = get_fs_type(fstype);
  1590. struct vfsmount *mnt;
  1591. if (!type)
  1592. return ERR_PTR(-ENODEV);
  1593. mnt = vfs_kern_mount(type, flags, name, data);
  1594. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  1595. !mnt->mnt_sb->s_subtype)
  1596. mnt = fs_set_subtype(mnt, fstype);
  1597. put_filesystem(type);
  1598. return mnt;
  1599. }
  1600. /*
  1601. * add a mount into a namespace's mount tree
  1602. */
  1603. static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
  1604. {
  1605. int err;
  1606. mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
  1607. err = lock_mount(path);
  1608. if (err)
  1609. return err;
  1610. err = -EINVAL;
  1611. if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(real_mount(path->mnt)))
  1612. goto unlock;
  1613. /* Refuse the same filesystem on the same mount point */
  1614. err = -EBUSY;
  1615. if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
  1616. path->mnt->mnt_root == path->dentry)
  1617. goto unlock;
  1618. err = -EINVAL;
  1619. if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))
  1620. goto unlock;
  1621. newmnt->mnt.mnt_flags = mnt_flags;
  1622. err = graft_tree(newmnt, path);
  1623. unlock:
  1624. unlock_mount(path);
  1625. return err;
  1626. }
  1627. /*
  1628. * create a new mount for userspace and request it to be added into the
  1629. * namespace's tree
  1630. */
  1631. static int do_new_mount(struct path *path, char *type, int flags,
  1632. int mnt_flags, char *name, void *data)
  1633. {
  1634. struct vfsmount *mnt;
  1635. int err;
  1636. if (!type)
  1637. return -EINVAL;
  1638. /* we need capabilities... */
  1639. if (!capable(CAP_SYS_ADMIN))
  1640. return -EPERM;
  1641. mnt = do_kern_mount(type, flags, name, data);
  1642. if (IS_ERR(mnt))
  1643. return PTR_ERR(mnt);
  1644. err = do_add_mount(real_mount(mnt), path, mnt_flags);
  1645. if (err)
  1646. mntput(mnt);
  1647. return err;
  1648. }
  1649. int finish_automount(struct vfsmount *m, struct path *path)
  1650. {
  1651. struct mount *mnt = real_mount(m);
  1652. int err;
  1653. /* The new mount record should have at least 2 refs to prevent it being
  1654. * expired before we get a chance to add it
  1655. */
  1656. BUG_ON(mnt_get_count(mnt) < 2);
  1657. if (m->mnt_sb == path->mnt->mnt_sb &&
  1658. m->mnt_root == path->dentry) {
  1659. err = -ELOOP;
  1660. goto fail;
  1661. }
  1662. err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
  1663. if (!err)
  1664. return 0;
  1665. fail:
  1666. /* remove m from any expiration list it may be on */
  1667. if (!list_empty(&mnt->mnt_expire)) {
  1668. down_write(&namespace_sem);
  1669. br_write_lock(&vfsmount_lock);
  1670. list_del_init(&mnt->mnt_expire);
  1671. br_write_unlock(&vfsmount_lock);
  1672. up_write(&namespace_sem);
  1673. }
  1674. mntput(m);
  1675. mntput(m);
  1676. return err;
  1677. }
  1678. /**
  1679. * mnt_set_expiry - Put a mount on an expiration list
  1680. * @mnt: The mount to list.
  1681. * @expiry_list: The list to add the mount to.
  1682. */
  1683. void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
  1684. {
  1685. down_write(&namespace_sem);
  1686. br_write_lock(&vfsmount_lock);
  1687. list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
  1688. br_write_unlock(&vfsmount_lock);
  1689. up_write(&namespace_sem);
  1690. }
  1691. EXPORT_SYMBOL(mnt_set_expiry);
  1692. /*
  1693. * process a list of expirable mountpoints with the intent of discarding any
  1694. * mountpoints that aren't in use and haven't been touched since last we came
  1695. * here
  1696. */
  1697. void mark_mounts_for_expiry(struct list_head *mounts)
  1698. {
  1699. struct mount *mnt, *next;
  1700. LIST_HEAD(graveyard);
  1701. LIST_HEAD(umounts);
  1702. if (list_empty(mounts))
  1703. return;
  1704. down_write(&namespace_sem);
  1705. br_write_lock(&vfsmount_lock);
  1706. /* extract from the expiration list every vfsmount that matches the
  1707. * following criteria:
  1708. * - only referenced by its parent vfsmount
  1709. * - still marked for expiry (marked on the last call here; marks are
  1710. * cleared by mntput())
  1711. */
  1712. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  1713. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  1714. propagate_mount_busy(mnt, 1))
  1715. continue;
  1716. list_move(&mnt->mnt_expire, &graveyard);
  1717. }
  1718. while (!list_empty(&graveyard)) {
  1719. mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
  1720. touch_mnt_namespace(mnt->mnt_ns);
  1721. umount_tree(mnt, 1, &umounts);
  1722. }
  1723. br_write_unlock(&vfsmount_lock);
  1724. up_write(&namespace_sem);
  1725. release_mounts(&umounts);
  1726. }
  1727. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  1728. /*
  1729. * Ripoff of 'select_parent()'
  1730. *
  1731. * search the list of submounts for a given mountpoint, and move any
  1732. * shrinkable submounts to the 'graveyard' list.
  1733. */
  1734. static int select_submounts(struct mount *parent, struct list_head *graveyard)
  1735. {
  1736. struct mount *this_parent = parent;
  1737. struct list_head *next;
  1738. int found = 0;
  1739. repeat:
  1740. next = this_parent->mnt_mounts.next;
  1741. resume:
  1742. while (next != &this_parent->mnt_mounts) {
  1743. struct list_head *tmp = next;
  1744. struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
  1745. next = tmp->next;
  1746. if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
  1747. continue;
  1748. /*
  1749. * Descend a level if the d_mounts list is non-empty.
  1750. */
  1751. if (!list_empty(&mnt->mnt_mounts)) {
  1752. this_parent = mnt;
  1753. goto repeat;
  1754. }
  1755. if (!propagate_mount_busy(mnt, 1)) {
  1756. list_move_tail(&mnt->mnt_expire, graveyard);
  1757. found++;
  1758. }
  1759. }
  1760. /*
  1761. * All done at this level ... ascend and resume the search
  1762. */
  1763. if (this_parent != parent) {
  1764. next = this_parent->mnt_child.next;
  1765. this_parent = this_parent->mnt_parent;
  1766. goto resume;
  1767. }
  1768. return found;
  1769. }
  1770. /*
  1771. * process a list of expirable mountpoints with the intent of discarding any
  1772. * submounts of a specific parent mountpoint
  1773. *
  1774. * vfsmount_lock must be held for write
  1775. */
  1776. static void shrink_submounts(struct mount *mnt, struct list_head *umounts)
  1777. {
  1778. LIST_HEAD(graveyard);
  1779. struct mount *m;
  1780. /* extract submounts of 'mountpoint' from the expiration list */
  1781. while (select_submounts(mnt, &graveyard)) {
  1782. while (!list_empty(&graveyard)) {
  1783. m = list_first_entry(&graveyard, struct mount,
  1784. mnt_expire);
  1785. touch_mnt_namespace(m->mnt_ns);
  1786. umount_tree(m, 1, umounts);
  1787. }
  1788. }
  1789. }
  1790. /*
  1791. * Some copy_from_user() implementations do not return the exact number of
  1792. * bytes remaining to copy on a fault. But copy_mount_options() requires that.
  1793. * Note that this function differs from copy_from_user() in that it will oops
  1794. * on bad values of `to', rather than returning a short copy.
  1795. */
  1796. static long exact_copy_from_user(void *to, const void __user * from,
  1797. unsigned long n)
  1798. {
  1799. char *t = to;
  1800. const char __user *f = from;
  1801. char c;
  1802. if (!access_ok(VERIFY_READ, from, n))
  1803. return n;
  1804. while (n) {
  1805. if (__get_user(c, f)) {
  1806. memset(t, 0, n);
  1807. break;
  1808. }
  1809. *t++ = c;
  1810. f++;
  1811. n--;
  1812. }
  1813. return n;
  1814. }
  1815. int copy_mount_options(const void __user * data, unsigned long *where)
  1816. {
  1817. int i;
  1818. unsigned long page;
  1819. unsigned long size;
  1820. *where = 0;
  1821. if (!data)
  1822. return 0;
  1823. if (!(page = __get_free_page(GFP_KERNEL)))
  1824. return -ENOMEM;
  1825. /* We only care that *some* data at the address the user
  1826. * gave us is valid. Just in case, we'll zero
  1827. * the remainder of the page.
  1828. */
  1829. /* copy_from_user cannot cross TASK_SIZE ! */
  1830. size = TASK_SIZE - (unsigned long)data;
  1831. if (size > PAGE_SIZE)
  1832. size = PAGE_SIZE;
  1833. i = size - exact_copy_from_user((void *)page, data, size);
  1834. if (!i) {
  1835. free_page(page);
  1836. return -EFAULT;
  1837. }
  1838. if (i != PAGE_SIZE)
  1839. memset((char *)page + i, 0, PAGE_SIZE - i);
  1840. *where = page;
  1841. return 0;
  1842. }
  1843. int copy_mount_string(const void __user *data, char **where)
  1844. {
  1845. char *tmp;
  1846. if (!data) {
  1847. *where = NULL;
  1848. return 0;
  1849. }
  1850. tmp = strndup_user(data, PAGE_SIZE);
  1851. if (IS_ERR(tmp))
  1852. return PTR_ERR(tmp);
  1853. *where = tmp;
  1854. return 0;
  1855. }
  1856. /*
  1857. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  1858. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  1859. *
  1860. * data is a (void *) that can point to any structure up to
  1861. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  1862. * information (or be NULL).
  1863. *
  1864. * Pre-0.97 versions of mount() didn't have a flags word.
  1865. * When the flags word was introduced its top half was required
  1866. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  1867. * Therefore, if this magic number is present, it carries no information
  1868. * and must be discarded.
  1869. */
  1870. long do_mount(char *dev_name, char *dir_name, char *type_page,
  1871. unsigned long flags, void *data_page)
  1872. {
  1873. struct path path;
  1874. int retval = 0;
  1875. int mnt_flags = 0;
  1876. /* Discard magic */
  1877. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  1878. flags &= ~MS_MGC_MSK;
  1879. /* Basic sanity checks */
  1880. if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
  1881. return -EINVAL;
  1882. if (data_page)
  1883. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  1884. /* ... and get the mountpoint */
  1885. retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
  1886. if (retval)
  1887. return retval;
  1888. retval = security_sb_mount(dev_name, &path,
  1889. type_page, flags, data_page);
  1890. if (retval)
  1891. goto dput_out;
  1892. /* Default to relatime unless overriden */
  1893. if (!(flags & MS_NOATIME))
  1894. mnt_flags |= MNT_RELATIME;
  1895. /* Separate the per-mountpoint flags */
  1896. if (flags & MS_NOSUID)
  1897. mnt_flags |= MNT_NOSUID;
  1898. if (flags & MS_NODEV)
  1899. mnt_flags |= MNT_NODEV;
  1900. if (flags & MS_NOEXEC)
  1901. mnt_flags |= MNT_NOEXEC;
  1902. if (flags & MS_NOATIME)
  1903. mnt_flags |= MNT_NOATIME;
  1904. if (flags & MS_NODIRATIME)
  1905. mnt_flags |= MNT_NODIRATIME;
  1906. if (flags & MS_STRICTATIME)
  1907. mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
  1908. if (flags & MS_RDONLY)
  1909. mnt_flags |= MNT_READONLY;
  1910. flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
  1911. MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
  1912. MS_STRICTATIME);
  1913. if (flags & MS_REMOUNT)
  1914. retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
  1915. data_page);
  1916. else if (flags & MS_BIND)
  1917. retval = do_loopback(&path, dev_name, flags & MS_REC);
  1918. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1919. retval = do_change_type(&path, flags);
  1920. else if (flags & MS_MOVE)
  1921. retval = do_move_mount(&path, dev_name);
  1922. else
  1923. retval = do_new_mount(&path, type_page, flags, mnt_flags,
  1924. dev_name, data_page);
  1925. dput_out:
  1926. path_put(&path);
  1927. return retval;
  1928. }
  1929. static struct mnt_namespace *alloc_mnt_ns(void)
  1930. {
  1931. struct mnt_namespace *new_ns;
  1932. new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
  1933. if (!new_ns)
  1934. return ERR_PTR(-ENOMEM);
  1935. atomic_set(&new_ns->count, 1);
  1936. new_ns->root = NULL;
  1937. INIT_LIST_HEAD(&new_ns->list);
  1938. init_waitqueue_head(&new_ns->poll);
  1939. new_ns->event = 0;
  1940. return new_ns;
  1941. }
  1942. void mnt_make_longterm(struct vfsmount *mnt)
  1943. {
  1944. __mnt_make_longterm(real_mount(mnt));
  1945. }
  1946. void mnt_make_shortterm(struct vfsmount *m)
  1947. {
  1948. #ifdef CONFIG_SMP
  1949. struct mount *mnt = real_mount(m);
  1950. if (atomic_add_unless(&mnt->mnt_longterm, -1, 1))
  1951. return;
  1952. br_write_lock(&vfsmount_lock);
  1953. atomic_dec(&mnt->mnt_longterm);
  1954. br_write_unlock(&vfsmount_lock);
  1955. #endif
  1956. }
  1957. /*
  1958. * Allocate a new namespace structure and populate it with contents
  1959. * copied from the namespace of the passed in task structure.
  1960. */
  1961. static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
  1962. struct fs_struct *fs)
  1963. {
  1964. struct mnt_namespace *new_ns;
  1965. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
  1966. struct mount *p, *q;
  1967. struct mount *old = mnt_ns->root;
  1968. struct mount *new;
  1969. new_ns = alloc_mnt_ns();
  1970. if (IS_ERR(new_ns))
  1971. return new_ns;
  1972. down_write(&namespace_sem);
  1973. /* First pass: copy the tree topology */
  1974. new = copy_tree(old, old->mnt.mnt_root, CL_COPY_ALL | CL_EXPIRE);
  1975. if (!new) {
  1976. up_write(&namespace_sem);
  1977. kfree(new_ns);
  1978. return ERR_PTR(-ENOMEM);
  1979. }
  1980. new_ns->root = new;
  1981. br_write_lock(&vfsmount_lock);
  1982. list_add_tail(&new_ns->list, &new->mnt_list);
  1983. br_write_unlock(&vfsmount_lock);
  1984. /*
  1985. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  1986. * as belonging to new namespace. We have already acquired a private
  1987. * fs_struct, so tsk->fs->lock is not needed.
  1988. */
  1989. p = old;
  1990. q = new;
  1991. while (p) {
  1992. q->mnt_ns = new_ns;
  1993. __mnt_make_longterm(q);
  1994. if (fs) {
  1995. if (&p->mnt == fs->root.mnt) {
  1996. fs->root.mnt = mntget(&q->mnt);
  1997. __mnt_make_longterm(q);
  1998. mnt_make_shortterm(&p->mnt);
  1999. rootmnt = &p->mnt;
  2000. }
  2001. if (&p->mnt == fs->pwd.mnt) {
  2002. fs->pwd.mnt = mntget(&q->mnt);
  2003. __mnt_make_longterm(q);
  2004. mnt_make_shortterm(&p->mnt);
  2005. pwdmnt = &p->mnt;
  2006. }
  2007. }
  2008. p = next_mnt(p, old);
  2009. q = next_mnt(q, new);
  2010. }
  2011. up_write(&namespace_sem);
  2012. if (rootmnt)
  2013. mntput(rootmnt);
  2014. if (pwdmnt)
  2015. mntput(pwdmnt);
  2016. return new_ns;
  2017. }
  2018. struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
  2019. struct fs_struct *new_fs)
  2020. {
  2021. struct mnt_namespace *new_ns;
  2022. BUG_ON(!ns);
  2023. get_mnt_ns(ns);
  2024. if (!(flags & CLONE_NEWNS))
  2025. return ns;
  2026. new_ns = dup_mnt_ns(ns, new_fs);
  2027. put_mnt_ns(ns);
  2028. return new_ns;
  2029. }
  2030. /**
  2031. * create_mnt_ns - creates a private namespace and adds a root filesystem
  2032. * @mnt: pointer to the new root filesystem mountpoint
  2033. */
  2034. static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
  2035. {
  2036. struct mnt_namespace *new_ns = alloc_mnt_ns();
  2037. if (!IS_ERR(new_ns)) {
  2038. struct mount *mnt = real_mount(m);
  2039. mnt->mnt_ns = new_ns;
  2040. __mnt_make_longterm(mnt);
  2041. new_ns->root = mnt;
  2042. list_add(&new_ns->list, &mnt->mnt_list);
  2043. } else {
  2044. mntput(m);
  2045. }
  2046. return new_ns;
  2047. }
  2048. struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
  2049. {
  2050. struct mnt_namespace *ns;
  2051. struct super_block *s;
  2052. struct path path;
  2053. int err;
  2054. ns = create_mnt_ns(mnt);
  2055. if (IS_ERR(ns))
  2056. return ERR_CAST(ns);
  2057. err = vfs_path_lookup(mnt->mnt_root, mnt,
  2058. name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  2059. put_mnt_ns(ns);
  2060. if (err)
  2061. return ERR_PTR(err);
  2062. /* trade a vfsmount reference for active sb one */
  2063. s = path.mnt->mnt_sb;
  2064. atomic_inc(&s->s_active);
  2065. mntput(path.mnt);
  2066. /* lock the sucker */
  2067. down_write(&s->s_umount);
  2068. /* ... and return the root of (sub)tree on it */
  2069. return path.dentry;
  2070. }
  2071. EXPORT_SYMBOL(mount_subtree);
  2072. SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
  2073. char __user *, type, unsigned long, flags, void __user *, data)
  2074. {
  2075. int ret;
  2076. char *kernel_type;
  2077. char *kernel_dir;
  2078. char *kernel_dev;
  2079. unsigned long data_page;
  2080. ret = copy_mount_string(type, &kernel_type);
  2081. if (ret < 0)
  2082. goto out_type;
  2083. kernel_dir = getname(dir_name);
  2084. if (IS_ERR(kernel_dir)) {
  2085. ret = PTR_ERR(kernel_dir);
  2086. goto out_dir;
  2087. }
  2088. ret = copy_mount_string(dev_name, &kernel_dev);
  2089. if (ret < 0)
  2090. goto out_dev;
  2091. ret = copy_mount_options(data, &data_page);
  2092. if (ret < 0)
  2093. goto out_data;
  2094. ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,
  2095. (void *) data_page);
  2096. free_page(data_page);
  2097. out_data:
  2098. kfree(kernel_dev);
  2099. out_dev:
  2100. putname(kernel_dir);
  2101. out_dir:
  2102. kfree(kernel_type);
  2103. out_type:
  2104. return ret;
  2105. }
  2106. /*
  2107. * Return true if path is reachable from root
  2108. *
  2109. * namespace_sem or vfsmount_lock is held
  2110. */
  2111. bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
  2112. const struct path *root)
  2113. {
  2114. while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
  2115. dentry = mnt->mnt_mountpoint;
  2116. mnt = mnt->mnt_parent;
  2117. }
  2118. return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
  2119. }
  2120. int path_is_under(struct path *path1, struct path *path2)
  2121. {
  2122. int res;
  2123. br_read_lock(&vfsmount_lock);
  2124. res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
  2125. br_read_unlock(&vfsmount_lock);
  2126. return res;
  2127. }
  2128. EXPORT_SYMBOL(path_is_under);
  2129. /*
  2130. * pivot_root Semantics:
  2131. * Moves the root file system of the current process to the directory put_old,
  2132. * makes new_root as the new root file system of the current process, and sets
  2133. * root/cwd of all processes which had them on the current root to new_root.
  2134. *
  2135. * Restrictions:
  2136. * The new_root and put_old must be directories, and must not be on the
  2137. * same file system as the current process root. The put_old must be
  2138. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  2139. * pointed to by put_old must yield the same directory as new_root. No other
  2140. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  2141. *
  2142. * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
  2143. * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
  2144. * in this situation.
  2145. *
  2146. * Notes:
  2147. * - we don't move root/cwd if they are not at the root (reason: if something
  2148. * cared enough to change them, it's probably wrong to force them elsewhere)
  2149. * - it's okay to pick a root that isn't the root of a file system, e.g.
  2150. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  2151. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  2152. * first.
  2153. */
  2154. SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
  2155. const char __user *, put_old)
  2156. {
  2157. struct path new, old, parent_path, root_parent, root;
  2158. struct mount *new_mnt, *root_mnt;
  2159. int error;
  2160. if (!capable(CAP_SYS_ADMIN))
  2161. return -EPERM;
  2162. error = user_path_dir(new_root, &new);
  2163. if (error)
  2164. goto out0;
  2165. error = user_path_dir(put_old, &old);
  2166. if (error)
  2167. goto out1;
  2168. error = security_sb_pivotroot(&old, &new);
  2169. if (error)
  2170. goto out2;
  2171. get_fs_root(current->fs, &root);
  2172. error = lock_mount(&old);
  2173. if (error)
  2174. goto out3;
  2175. error = -EINVAL;
  2176. new_mnt = real_mount(new.mnt);
  2177. root_mnt = real_mount(root.mnt);
  2178. if (IS_MNT_SHARED(real_mount(old.mnt)) ||
  2179. IS_MNT_SHARED(new_mnt->mnt_parent) ||
  2180. IS_MNT_SHARED(root_mnt->mnt_parent))
  2181. goto out4;
  2182. if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
  2183. goto out4;
  2184. error = -ENOENT;
  2185. if (d_unlinked(new.dentry))
  2186. goto out4;
  2187. if (d_unlinked(old.dentry))
  2188. goto out4;
  2189. error = -EBUSY;
  2190. if (new.mnt == root.mnt ||
  2191. old.mnt == root.mnt)
  2192. goto out4; /* loop, on the same file system */
  2193. error = -EINVAL;
  2194. if (root.mnt->mnt_root != root.dentry)
  2195. goto out4; /* not a mountpoint */
  2196. if (!mnt_has_parent(root_mnt))
  2197. goto out4; /* not attached */
  2198. if (new.mnt->mnt_root != new.dentry)
  2199. goto out4; /* not a mountpoint */
  2200. if (!mnt_has_parent(new_mnt))
  2201. goto out4; /* not attached */
  2202. /* make sure we can reach put_old from new_root */
  2203. if (!is_path_reachable(real_mount(old.mnt), old.dentry, &new))
  2204. goto out4;
  2205. br_write_lock(&vfsmount_lock);
  2206. detach_mnt(new_mnt, &parent_path);
  2207. detach_mnt(root_mnt, &root_parent);
  2208. /* mount old root on put_old */
  2209. attach_mnt(root_mnt, &old);
  2210. /* mount new_root on / */
  2211. attach_mnt(new_mnt, &root_parent);
  2212. touch_mnt_namespace(current->nsproxy->mnt_ns);
  2213. br_write_unlock(&vfsmount_lock);
  2214. chroot_fs_refs(&root, &new);
  2215. error = 0;
  2216. out4:
  2217. unlock_mount(&old);
  2218. if (!error) {
  2219. path_put(&root_parent);
  2220. path_put(&parent_path);
  2221. }
  2222. out3:
  2223. path_put(&root);
  2224. out2:
  2225. path_put(&old);
  2226. out1:
  2227. path_put(&new);
  2228. out0:
  2229. return error;
  2230. }
  2231. static void __init init_mount_tree(void)
  2232. {
  2233. struct vfsmount *mnt;
  2234. struct mnt_namespace *ns;
  2235. struct path root;
  2236. mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
  2237. if (IS_ERR(mnt))
  2238. panic("Can't create rootfs");
  2239. ns = create_mnt_ns(mnt);
  2240. if (IS_ERR(ns))
  2241. panic("Can't allocate initial namespace");
  2242. init_task.nsproxy->mnt_ns = ns;
  2243. get_mnt_ns(ns);
  2244. root.mnt = mnt;
  2245. root.dentry = mnt->mnt_root;
  2246. set_fs_pwd(current->fs, &root);
  2247. set_fs_root(current->fs, &root);
  2248. }
  2249. void __init mnt_init(void)
  2250. {
  2251. unsigned u;
  2252. int err;
  2253. init_rwsem(&namespace_sem);
  2254. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
  2255. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2256. mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
  2257. if (!mount_hashtable)
  2258. panic("Failed to allocate mount hash table\n");
  2259. printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);
  2260. for (u = 0; u < HASH_SIZE; u++)
  2261. INIT_LIST_HEAD(&mount_hashtable[u]);
  2262. br_lock_init(&vfsmount_lock);
  2263. err = sysfs_init();
  2264. if (err)
  2265. printk(KERN_WARNING "%s: sysfs_init error: %d\n",
  2266. __func__, err);
  2267. fs_kobj = kobject_create_and_add("fs", NULL);
  2268. if (!fs_kobj)
  2269. printk(KERN_WARNING "%s: kobj create error\n", __func__);
  2270. init_rootfs();
  2271. init_mount_tree();
  2272. }
  2273. void put_mnt_ns(struct mnt_namespace *ns)
  2274. {
  2275. LIST_HEAD(umount_list);
  2276. if (!atomic_dec_and_test(&ns->count))
  2277. return;
  2278. down_write(&namespace_sem);
  2279. br_write_lock(&vfsmount_lock);
  2280. umount_tree(ns->root, 0, &umount_list);
  2281. br_write_unlock(&vfsmount_lock);
  2282. up_write(&namespace_sem);
  2283. release_mounts(&umount_list);
  2284. kfree(ns);
  2285. }
  2286. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  2287. {
  2288. struct vfsmount *mnt;
  2289. mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  2290. if (!IS_ERR(mnt)) {
  2291. /*
  2292. * it is a longterm mount, don't release mnt until
  2293. * we unmount before file sys is unregistered
  2294. */
  2295. mnt_make_longterm(mnt);
  2296. }
  2297. return mnt;
  2298. }
  2299. EXPORT_SYMBOL_GPL(kern_mount_data);
  2300. void kern_unmount(struct vfsmount *mnt)
  2301. {
  2302. /* release long term mount so mount point can be released */
  2303. if (!IS_ERR_OR_NULL(mnt)) {
  2304. mnt_make_shortterm(mnt);
  2305. mntput(mnt);
  2306. }
  2307. }
  2308. EXPORT_SYMBOL(kern_unmount);
  2309. bool our_mnt(struct vfsmount *mnt)
  2310. {
  2311. return check_mnt(real_mount(mnt));
  2312. }