namespace.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456
  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/user_namespace.h>
  15. #include <linux/namei.h>
  16. #include <linux/security.h>
  17. #include <linux/idr.h>
  18. #include <linux/init.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 <linux/proc_ns.h>
  23. #include <linux/magic.h>
  24. #include <linux/bootmem.h>
  25. #include <linux/task_work.h>
  26. #include "pnode.h"
  27. #include "internal.h"
  28. /* Maximum number of mounts in a mount namespace */
  29. unsigned int sysctl_mount_max __read_mostly = 100000;
  30. static unsigned int m_hash_mask __read_mostly;
  31. static unsigned int m_hash_shift __read_mostly;
  32. static unsigned int mp_hash_mask __read_mostly;
  33. static unsigned int mp_hash_shift __read_mostly;
  34. static __initdata unsigned long mhash_entries;
  35. static int __init set_mhash_entries(char *str)
  36. {
  37. if (!str)
  38. return 0;
  39. mhash_entries = simple_strtoul(str, &str, 0);
  40. return 1;
  41. }
  42. __setup("mhash_entries=", set_mhash_entries);
  43. static __initdata unsigned long mphash_entries;
  44. static int __init set_mphash_entries(char *str)
  45. {
  46. if (!str)
  47. return 0;
  48. mphash_entries = simple_strtoul(str, &str, 0);
  49. return 1;
  50. }
  51. __setup("mphash_entries=", set_mphash_entries);
  52. static u64 event;
  53. static DEFINE_IDA(mnt_id_ida);
  54. static DEFINE_IDA(mnt_group_ida);
  55. static DEFINE_SPINLOCK(mnt_id_lock);
  56. static int mnt_id_start = 0;
  57. static int mnt_group_start = 1;
  58. static struct hlist_head *mount_hashtable __read_mostly;
  59. static struct hlist_head *mountpoint_hashtable __read_mostly;
  60. static struct kmem_cache *mnt_cache __read_mostly;
  61. static DECLARE_RWSEM(namespace_sem);
  62. /* /sys/fs */
  63. struct kobject *fs_kobj;
  64. EXPORT_SYMBOL_GPL(fs_kobj);
  65. /*
  66. * vfsmount lock may be taken for read to prevent changes to the
  67. * vfsmount hash, ie. during mountpoint lookups or walking back
  68. * up the tree.
  69. *
  70. * It should be taken for write in all cases where the vfsmount
  71. * tree or hash is modified or when a vfsmount structure is modified.
  72. */
  73. __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
  74. static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
  75. {
  76. unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
  77. tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
  78. tmp = tmp + (tmp >> m_hash_shift);
  79. return &mount_hashtable[tmp & m_hash_mask];
  80. }
  81. static inline struct hlist_head *mp_hash(struct dentry *dentry)
  82. {
  83. unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
  84. tmp = tmp + (tmp >> mp_hash_shift);
  85. return &mountpoint_hashtable[tmp & mp_hash_mask];
  86. }
  87. static int mnt_alloc_id(struct mount *mnt)
  88. {
  89. int res;
  90. retry:
  91. ida_pre_get(&mnt_id_ida, GFP_KERNEL);
  92. spin_lock(&mnt_id_lock);
  93. res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
  94. if (!res)
  95. mnt_id_start = mnt->mnt_id + 1;
  96. spin_unlock(&mnt_id_lock);
  97. if (res == -EAGAIN)
  98. goto retry;
  99. return res;
  100. }
  101. static void mnt_free_id(struct mount *mnt)
  102. {
  103. int id = mnt->mnt_id;
  104. spin_lock(&mnt_id_lock);
  105. ida_remove(&mnt_id_ida, id);
  106. if (mnt_id_start > id)
  107. mnt_id_start = id;
  108. spin_unlock(&mnt_id_lock);
  109. }
  110. /*
  111. * Allocate a new peer group ID
  112. *
  113. * mnt_group_ida is protected by namespace_sem
  114. */
  115. static int mnt_alloc_group_id(struct mount *mnt)
  116. {
  117. int res;
  118. if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
  119. return -ENOMEM;
  120. res = ida_get_new_above(&mnt_group_ida,
  121. mnt_group_start,
  122. &mnt->mnt_group_id);
  123. if (!res)
  124. mnt_group_start = mnt->mnt_group_id + 1;
  125. return res;
  126. }
  127. /*
  128. * Release a peer group ID
  129. */
  130. void mnt_release_group_id(struct mount *mnt)
  131. {
  132. int id = mnt->mnt_group_id;
  133. ida_remove(&mnt_group_ida, id);
  134. if (mnt_group_start > id)
  135. mnt_group_start = id;
  136. mnt->mnt_group_id = 0;
  137. }
  138. /*
  139. * vfsmount lock must be held for read
  140. */
  141. static inline void mnt_add_count(struct mount *mnt, int n)
  142. {
  143. #ifdef CONFIG_SMP
  144. this_cpu_add(mnt->mnt_pcp->mnt_count, n);
  145. #else
  146. preempt_disable();
  147. mnt->mnt_count += n;
  148. preempt_enable();
  149. #endif
  150. }
  151. /*
  152. * vfsmount lock must be held for write
  153. */
  154. unsigned int mnt_get_count(struct mount *mnt)
  155. {
  156. #ifdef CONFIG_SMP
  157. unsigned int count = 0;
  158. int cpu;
  159. for_each_possible_cpu(cpu) {
  160. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
  161. }
  162. return count;
  163. #else
  164. return mnt->mnt_count;
  165. #endif
  166. }
  167. static void drop_mountpoint(struct fs_pin *p)
  168. {
  169. struct mount *m = container_of(p, struct mount, mnt_umount);
  170. dput(m->mnt_ex_mountpoint);
  171. pin_remove(p);
  172. mntput(&m->mnt);
  173. }
  174. static struct mount *alloc_vfsmnt(const char *name)
  175. {
  176. struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
  177. if (mnt) {
  178. int err;
  179. err = mnt_alloc_id(mnt);
  180. if (err)
  181. goto out_free_cache;
  182. if (name) {
  183. mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
  184. if (!mnt->mnt_devname)
  185. goto out_free_id;
  186. }
  187. #ifdef CONFIG_SMP
  188. mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
  189. if (!mnt->mnt_pcp)
  190. goto out_free_devname;
  191. this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
  192. #else
  193. mnt->mnt_count = 1;
  194. mnt->mnt_writers = 0;
  195. #endif
  196. INIT_HLIST_NODE(&mnt->mnt_hash);
  197. INIT_LIST_HEAD(&mnt->mnt_child);
  198. INIT_LIST_HEAD(&mnt->mnt_mounts);
  199. INIT_LIST_HEAD(&mnt->mnt_list);
  200. INIT_LIST_HEAD(&mnt->mnt_expire);
  201. INIT_LIST_HEAD(&mnt->mnt_share);
  202. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  203. INIT_LIST_HEAD(&mnt->mnt_slave);
  204. INIT_HLIST_NODE(&mnt->mnt_mp_list);
  205. #ifdef CONFIG_FSNOTIFY
  206. INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
  207. #endif
  208. init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
  209. }
  210. return mnt;
  211. #ifdef CONFIG_SMP
  212. out_free_devname:
  213. kfree_const(mnt->mnt_devname);
  214. #endif
  215. out_free_id:
  216. mnt_free_id(mnt);
  217. out_free_cache:
  218. kmem_cache_free(mnt_cache, mnt);
  219. return NULL;
  220. }
  221. /*
  222. * Most r/o checks on a fs are for operations that take
  223. * discrete amounts of time, like a write() or unlink().
  224. * We must keep track of when those operations start
  225. * (for permission checks) and when they end, so that
  226. * we can determine when writes are able to occur to
  227. * a filesystem.
  228. */
  229. /*
  230. * __mnt_is_readonly: check whether a mount is read-only
  231. * @mnt: the mount to check for its write status
  232. *
  233. * This shouldn't be used directly ouside of the VFS.
  234. * It does not guarantee that the filesystem will stay
  235. * r/w, just that it is right *now*. This can not and
  236. * should not be used in place of IS_RDONLY(inode).
  237. * mnt_want/drop_write() will _keep_ the filesystem
  238. * r/w.
  239. */
  240. int __mnt_is_readonly(struct vfsmount *mnt)
  241. {
  242. if (mnt->mnt_flags & MNT_READONLY)
  243. return 1;
  244. if (mnt->mnt_sb->s_flags & MS_RDONLY)
  245. return 1;
  246. return 0;
  247. }
  248. EXPORT_SYMBOL_GPL(__mnt_is_readonly);
  249. static inline void mnt_inc_writers(struct mount *mnt)
  250. {
  251. #ifdef CONFIG_SMP
  252. this_cpu_inc(mnt->mnt_pcp->mnt_writers);
  253. #else
  254. mnt->mnt_writers++;
  255. #endif
  256. }
  257. static inline void mnt_dec_writers(struct mount *mnt)
  258. {
  259. #ifdef CONFIG_SMP
  260. this_cpu_dec(mnt->mnt_pcp->mnt_writers);
  261. #else
  262. mnt->mnt_writers--;
  263. #endif
  264. }
  265. static unsigned int mnt_get_writers(struct mount *mnt)
  266. {
  267. #ifdef CONFIG_SMP
  268. unsigned int count = 0;
  269. int cpu;
  270. for_each_possible_cpu(cpu) {
  271. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
  272. }
  273. return count;
  274. #else
  275. return mnt->mnt_writers;
  276. #endif
  277. }
  278. static int mnt_is_readonly(struct vfsmount *mnt)
  279. {
  280. if (mnt->mnt_sb->s_readonly_remount)
  281. return 1;
  282. /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
  283. smp_rmb();
  284. return __mnt_is_readonly(mnt);
  285. }
  286. /*
  287. * Most r/o & frozen checks on a fs are for operations that take discrete
  288. * amounts of time, like a write() or unlink(). We must keep track of when
  289. * those operations start (for permission checks) and when they end, so that we
  290. * can determine when writes are able to occur to a filesystem.
  291. */
  292. /**
  293. * __mnt_want_write - get write access to a mount without freeze protection
  294. * @m: the mount on which to take a write
  295. *
  296. * This tells the low-level filesystem that a write is about to be performed to
  297. * it, and makes sure that writes are allowed (mnt it read-write) before
  298. * returning success. This operation does not protect against filesystem being
  299. * frozen. When the write operation is finished, __mnt_drop_write() must be
  300. * called. This is effectively a refcount.
  301. */
  302. int __mnt_want_write(struct vfsmount *m)
  303. {
  304. struct mount *mnt = real_mount(m);
  305. int ret = 0;
  306. preempt_disable();
  307. mnt_inc_writers(mnt);
  308. /*
  309. * The store to mnt_inc_writers must be visible before we pass
  310. * MNT_WRITE_HOLD loop below, so that the slowpath can see our
  311. * incremented count after it has set MNT_WRITE_HOLD.
  312. */
  313. smp_mb();
  314. while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  315. cpu_relax();
  316. /*
  317. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  318. * be set to match its requirements. So we must not load that until
  319. * MNT_WRITE_HOLD is cleared.
  320. */
  321. smp_rmb();
  322. if (mnt_is_readonly(m)) {
  323. mnt_dec_writers(mnt);
  324. ret = -EROFS;
  325. }
  326. preempt_enable();
  327. return ret;
  328. }
  329. /**
  330. * mnt_want_write - get write access to a mount
  331. * @m: the mount on which to take a write
  332. *
  333. * This tells the low-level filesystem that a write is about to be performed to
  334. * it, and makes sure that writes are allowed (mount is read-write, filesystem
  335. * is not frozen) before returning success. When the write operation is
  336. * finished, mnt_drop_write() must be called. This is effectively a refcount.
  337. */
  338. int mnt_want_write(struct vfsmount *m)
  339. {
  340. int ret;
  341. sb_start_write(m->mnt_sb);
  342. ret = __mnt_want_write(m);
  343. if (ret)
  344. sb_end_write(m->mnt_sb);
  345. return ret;
  346. }
  347. EXPORT_SYMBOL_GPL(mnt_want_write);
  348. /**
  349. * mnt_clone_write - get write access to a mount
  350. * @mnt: the mount on which to take a write
  351. *
  352. * This is effectively like mnt_want_write, except
  353. * it must only be used to take an extra write reference
  354. * on a mountpoint that we already know has a write reference
  355. * on it. This allows some optimisation.
  356. *
  357. * After finished, mnt_drop_write must be called as usual to
  358. * drop the reference.
  359. */
  360. int mnt_clone_write(struct vfsmount *mnt)
  361. {
  362. /* superblock may be r/o */
  363. if (__mnt_is_readonly(mnt))
  364. return -EROFS;
  365. preempt_disable();
  366. mnt_inc_writers(real_mount(mnt));
  367. preempt_enable();
  368. return 0;
  369. }
  370. EXPORT_SYMBOL_GPL(mnt_clone_write);
  371. /**
  372. * __mnt_want_write_file - get write access to a file's mount
  373. * @file: the file who's mount on which to take a write
  374. *
  375. * This is like __mnt_want_write, but it takes a file and can
  376. * do some optimisations if the file is open for write already
  377. */
  378. int __mnt_want_write_file(struct file *file)
  379. {
  380. if (!(file->f_mode & FMODE_WRITER))
  381. return __mnt_want_write(file->f_path.mnt);
  382. else
  383. return mnt_clone_write(file->f_path.mnt);
  384. }
  385. /**
  386. * mnt_want_write_file - get write access to a file's mount
  387. * @file: the file who's mount on which to take a write
  388. *
  389. * This is like mnt_want_write, but it takes a file and can
  390. * do some optimisations if the file is open for write already
  391. */
  392. int mnt_want_write_file(struct file *file)
  393. {
  394. int ret;
  395. sb_start_write(file->f_path.mnt->mnt_sb);
  396. ret = __mnt_want_write_file(file);
  397. if (ret)
  398. sb_end_write(file->f_path.mnt->mnt_sb);
  399. return ret;
  400. }
  401. EXPORT_SYMBOL_GPL(mnt_want_write_file);
  402. /**
  403. * __mnt_drop_write - give up write access to a mount
  404. * @mnt: the mount on which to give up write access
  405. *
  406. * Tells the low-level filesystem that we are done
  407. * performing writes to it. Must be matched with
  408. * __mnt_want_write() call above.
  409. */
  410. void __mnt_drop_write(struct vfsmount *mnt)
  411. {
  412. preempt_disable();
  413. mnt_dec_writers(real_mount(mnt));
  414. preempt_enable();
  415. }
  416. /**
  417. * mnt_drop_write - give up write access to a mount
  418. * @mnt: the mount on which to give up write access
  419. *
  420. * Tells the low-level filesystem that we are done performing writes to it and
  421. * also allows filesystem to be frozen again. Must be matched with
  422. * mnt_want_write() call above.
  423. */
  424. void mnt_drop_write(struct vfsmount *mnt)
  425. {
  426. __mnt_drop_write(mnt);
  427. sb_end_write(mnt->mnt_sb);
  428. }
  429. EXPORT_SYMBOL_GPL(mnt_drop_write);
  430. void __mnt_drop_write_file(struct file *file)
  431. {
  432. __mnt_drop_write(file->f_path.mnt);
  433. }
  434. void mnt_drop_write_file(struct file *file)
  435. {
  436. mnt_drop_write(file->f_path.mnt);
  437. }
  438. EXPORT_SYMBOL(mnt_drop_write_file);
  439. static int mnt_make_readonly(struct mount *mnt)
  440. {
  441. int ret = 0;
  442. lock_mount_hash();
  443. mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
  444. /*
  445. * After storing MNT_WRITE_HOLD, we'll read the counters. This store
  446. * should be visible before we do.
  447. */
  448. smp_mb();
  449. /*
  450. * With writers on hold, if this value is zero, then there are
  451. * definitely no active writers (although held writers may subsequently
  452. * increment the count, they'll have to wait, and decrement it after
  453. * seeing MNT_READONLY).
  454. *
  455. * It is OK to have counter incremented on one CPU and decremented on
  456. * another: the sum will add up correctly. The danger would be when we
  457. * sum up each counter, if we read a counter before it is incremented,
  458. * but then read another CPU's count which it has been subsequently
  459. * decremented from -- we would see more decrements than we should.
  460. * MNT_WRITE_HOLD protects against this scenario, because
  461. * mnt_want_write first increments count, then smp_mb, then spins on
  462. * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
  463. * we're counting up here.
  464. */
  465. if (mnt_get_writers(mnt) > 0)
  466. ret = -EBUSY;
  467. else
  468. mnt->mnt.mnt_flags |= MNT_READONLY;
  469. /*
  470. * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
  471. * that become unheld will see MNT_READONLY.
  472. */
  473. smp_wmb();
  474. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  475. unlock_mount_hash();
  476. return ret;
  477. }
  478. static void __mnt_unmake_readonly(struct mount *mnt)
  479. {
  480. lock_mount_hash();
  481. mnt->mnt.mnt_flags &= ~MNT_READONLY;
  482. unlock_mount_hash();
  483. }
  484. int sb_prepare_remount_readonly(struct super_block *sb)
  485. {
  486. struct mount *mnt;
  487. int err = 0;
  488. /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
  489. if (atomic_long_read(&sb->s_remove_count))
  490. return -EBUSY;
  491. lock_mount_hash();
  492. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  493. if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
  494. mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
  495. smp_mb();
  496. if (mnt_get_writers(mnt) > 0) {
  497. err = -EBUSY;
  498. break;
  499. }
  500. }
  501. }
  502. if (!err && atomic_long_read(&sb->s_remove_count))
  503. err = -EBUSY;
  504. if (!err) {
  505. sb->s_readonly_remount = 1;
  506. smp_wmb();
  507. }
  508. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  509. if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
  510. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  511. }
  512. unlock_mount_hash();
  513. return err;
  514. }
  515. static void free_vfsmnt(struct mount *mnt)
  516. {
  517. kfree_const(mnt->mnt_devname);
  518. #ifdef CONFIG_SMP
  519. free_percpu(mnt->mnt_pcp);
  520. #endif
  521. kmem_cache_free(mnt_cache, mnt);
  522. }
  523. static void delayed_free_vfsmnt(struct rcu_head *head)
  524. {
  525. free_vfsmnt(container_of(head, struct mount, mnt_rcu));
  526. }
  527. /* call under rcu_read_lock */
  528. int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
  529. {
  530. struct mount *mnt;
  531. if (read_seqretry(&mount_lock, seq))
  532. return 1;
  533. if (bastard == NULL)
  534. return 0;
  535. mnt = real_mount(bastard);
  536. mnt_add_count(mnt, 1);
  537. if (likely(!read_seqretry(&mount_lock, seq)))
  538. return 0;
  539. if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
  540. mnt_add_count(mnt, -1);
  541. return 1;
  542. }
  543. return -1;
  544. }
  545. /* call under rcu_read_lock */
  546. bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
  547. {
  548. int res = __legitimize_mnt(bastard, seq);
  549. if (likely(!res))
  550. return true;
  551. if (unlikely(res < 0)) {
  552. rcu_read_unlock();
  553. mntput(bastard);
  554. rcu_read_lock();
  555. }
  556. return false;
  557. }
  558. /*
  559. * find the first mount at @dentry on vfsmount @mnt.
  560. * call under rcu_read_lock()
  561. */
  562. struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
  563. {
  564. struct hlist_head *head = m_hash(mnt, dentry);
  565. struct mount *p;
  566. hlist_for_each_entry_rcu(p, head, mnt_hash)
  567. if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
  568. return p;
  569. return NULL;
  570. }
  571. /*
  572. * find the last mount at @dentry on vfsmount @mnt.
  573. * mount_lock must be held.
  574. */
  575. struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry)
  576. {
  577. struct mount *p, *res = NULL;
  578. p = __lookup_mnt(mnt, dentry);
  579. if (!p)
  580. goto out;
  581. if (!(p->mnt.mnt_flags & MNT_UMOUNT))
  582. res = p;
  583. hlist_for_each_entry_continue(p, mnt_hash) {
  584. if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry)
  585. break;
  586. if (!(p->mnt.mnt_flags & MNT_UMOUNT))
  587. res = p;
  588. }
  589. out:
  590. return res;
  591. }
  592. /*
  593. * lookup_mnt - Return the first child mount mounted at path
  594. *
  595. * "First" means first mounted chronologically. If you create the
  596. * following mounts:
  597. *
  598. * mount /dev/sda1 /mnt
  599. * mount /dev/sda2 /mnt
  600. * mount /dev/sda3 /mnt
  601. *
  602. * Then lookup_mnt() on the base /mnt dentry in the root mount will
  603. * return successively the root dentry and vfsmount of /dev/sda1, then
  604. * /dev/sda2, then /dev/sda3, then NULL.
  605. *
  606. * lookup_mnt takes a reference to the found vfsmount.
  607. */
  608. struct vfsmount *lookup_mnt(const struct path *path)
  609. {
  610. struct mount *child_mnt;
  611. struct vfsmount *m;
  612. unsigned seq;
  613. rcu_read_lock();
  614. do {
  615. seq = read_seqbegin(&mount_lock);
  616. child_mnt = __lookup_mnt(path->mnt, path->dentry);
  617. m = child_mnt ? &child_mnt->mnt : NULL;
  618. } while (!legitimize_mnt(m, seq));
  619. rcu_read_unlock();
  620. return m;
  621. }
  622. /*
  623. * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
  624. * current mount namespace.
  625. *
  626. * The common case is dentries are not mountpoints at all and that
  627. * test is handled inline. For the slow case when we are actually
  628. * dealing with a mountpoint of some kind, walk through all of the
  629. * mounts in the current mount namespace and test to see if the dentry
  630. * is a mountpoint.
  631. *
  632. * The mount_hashtable is not usable in the context because we
  633. * need to identify all mounts that may be in the current mount
  634. * namespace not just a mount that happens to have some specified
  635. * parent mount.
  636. */
  637. bool __is_local_mountpoint(struct dentry *dentry)
  638. {
  639. struct mnt_namespace *ns = current->nsproxy->mnt_ns;
  640. struct mount *mnt;
  641. bool is_covered = false;
  642. if (!d_mountpoint(dentry))
  643. goto out;
  644. down_read(&namespace_sem);
  645. list_for_each_entry(mnt, &ns->list, mnt_list) {
  646. is_covered = (mnt->mnt_mountpoint == dentry);
  647. if (is_covered)
  648. break;
  649. }
  650. up_read(&namespace_sem);
  651. out:
  652. return is_covered;
  653. }
  654. static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
  655. {
  656. struct hlist_head *chain = mp_hash(dentry);
  657. struct mountpoint *mp;
  658. hlist_for_each_entry(mp, chain, m_hash) {
  659. if (mp->m_dentry == dentry) {
  660. /* might be worth a WARN_ON() */
  661. if (d_unlinked(dentry))
  662. return ERR_PTR(-ENOENT);
  663. mp->m_count++;
  664. return mp;
  665. }
  666. }
  667. return NULL;
  668. }
  669. static struct mountpoint *new_mountpoint(struct dentry *dentry)
  670. {
  671. struct hlist_head *chain = mp_hash(dentry);
  672. struct mountpoint *mp;
  673. int ret;
  674. mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
  675. if (!mp)
  676. return ERR_PTR(-ENOMEM);
  677. ret = d_set_mounted(dentry);
  678. if (ret) {
  679. kfree(mp);
  680. return ERR_PTR(ret);
  681. }
  682. mp->m_dentry = dentry;
  683. mp->m_count = 1;
  684. hlist_add_head(&mp->m_hash, chain);
  685. INIT_HLIST_HEAD(&mp->m_list);
  686. return mp;
  687. }
  688. static void put_mountpoint(struct mountpoint *mp)
  689. {
  690. if (!--mp->m_count) {
  691. struct dentry *dentry = mp->m_dentry;
  692. BUG_ON(!hlist_empty(&mp->m_list));
  693. spin_lock(&dentry->d_lock);
  694. dentry->d_flags &= ~DCACHE_MOUNTED;
  695. spin_unlock(&dentry->d_lock);
  696. hlist_del(&mp->m_hash);
  697. kfree(mp);
  698. }
  699. }
  700. static inline int check_mnt(struct mount *mnt)
  701. {
  702. return mnt->mnt_ns == current->nsproxy->mnt_ns;
  703. }
  704. /*
  705. * vfsmount lock must be held for write
  706. */
  707. static void touch_mnt_namespace(struct mnt_namespace *ns)
  708. {
  709. if (ns) {
  710. ns->event = ++event;
  711. wake_up_interruptible(&ns->poll);
  712. }
  713. }
  714. /*
  715. * vfsmount lock must be held for write
  716. */
  717. static void __touch_mnt_namespace(struct mnt_namespace *ns)
  718. {
  719. if (ns && ns->event != event) {
  720. ns->event = event;
  721. wake_up_interruptible(&ns->poll);
  722. }
  723. }
  724. /*
  725. * vfsmount lock must be held for write
  726. */
  727. static void unhash_mnt(struct mount *mnt)
  728. {
  729. mnt->mnt_parent = mnt;
  730. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  731. list_del_init(&mnt->mnt_child);
  732. hlist_del_init_rcu(&mnt->mnt_hash);
  733. hlist_del_init(&mnt->mnt_mp_list);
  734. put_mountpoint(mnt->mnt_mp);
  735. mnt->mnt_mp = NULL;
  736. }
  737. /*
  738. * vfsmount lock must be held for write
  739. */
  740. static void detach_mnt(struct mount *mnt, struct path *old_path)
  741. {
  742. old_path->dentry = mnt->mnt_mountpoint;
  743. old_path->mnt = &mnt->mnt_parent->mnt;
  744. unhash_mnt(mnt);
  745. }
  746. /*
  747. * vfsmount lock must be held for write
  748. */
  749. static void umount_mnt(struct mount *mnt)
  750. {
  751. /* old mountpoint will be dropped when we can do that */
  752. mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
  753. unhash_mnt(mnt);
  754. }
  755. /*
  756. * vfsmount lock must be held for write
  757. */
  758. void mnt_set_mountpoint(struct mount *mnt,
  759. struct mountpoint *mp,
  760. struct mount *child_mnt)
  761. {
  762. mp->m_count++;
  763. mnt_add_count(mnt, 1); /* essentially, that's mntget */
  764. child_mnt->mnt_mountpoint = dget(mp->m_dentry);
  765. child_mnt->mnt_parent = mnt;
  766. child_mnt->mnt_mp = mp;
  767. hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
  768. }
  769. /*
  770. * vfsmount lock must be held for write
  771. */
  772. static void attach_mnt(struct mount *mnt,
  773. struct mount *parent,
  774. struct mountpoint *mp)
  775. {
  776. mnt_set_mountpoint(parent, mp, mnt);
  777. hlist_add_head_rcu(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry));
  778. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  779. }
  780. static void attach_shadowed(struct mount *mnt,
  781. struct mount *parent,
  782. struct mount *shadows)
  783. {
  784. if (shadows) {
  785. hlist_add_behind_rcu(&mnt->mnt_hash, &shadows->mnt_hash);
  786. list_add(&mnt->mnt_child, &shadows->mnt_child);
  787. } else {
  788. hlist_add_head_rcu(&mnt->mnt_hash,
  789. m_hash(&parent->mnt, mnt->mnt_mountpoint));
  790. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  791. }
  792. }
  793. /*
  794. * vfsmount lock must be held for write
  795. */
  796. static void commit_tree(struct mount *mnt, struct mount *shadows)
  797. {
  798. struct mount *parent = mnt->mnt_parent;
  799. struct mount *m;
  800. LIST_HEAD(head);
  801. struct mnt_namespace *n = parent->mnt_ns;
  802. BUG_ON(parent == mnt);
  803. list_add_tail(&head, &mnt->mnt_list);
  804. list_for_each_entry(m, &head, mnt_list)
  805. m->mnt_ns = n;
  806. list_splice(&head, n->list.prev);
  807. n->mounts += n->pending_mounts;
  808. n->pending_mounts = 0;
  809. attach_shadowed(mnt, parent, shadows);
  810. touch_mnt_namespace(n);
  811. }
  812. static struct mount *next_mnt(struct mount *p, struct mount *root)
  813. {
  814. struct list_head *next = p->mnt_mounts.next;
  815. if (next == &p->mnt_mounts) {
  816. while (1) {
  817. if (p == root)
  818. return NULL;
  819. next = p->mnt_child.next;
  820. if (next != &p->mnt_parent->mnt_mounts)
  821. break;
  822. p = p->mnt_parent;
  823. }
  824. }
  825. return list_entry(next, struct mount, mnt_child);
  826. }
  827. static struct mount *skip_mnt_tree(struct mount *p)
  828. {
  829. struct list_head *prev = p->mnt_mounts.prev;
  830. while (prev != &p->mnt_mounts) {
  831. p = list_entry(prev, struct mount, mnt_child);
  832. prev = p->mnt_mounts.prev;
  833. }
  834. return p;
  835. }
  836. struct vfsmount *
  837. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  838. {
  839. struct mount *mnt;
  840. struct dentry *root;
  841. if (!type)
  842. return ERR_PTR(-ENODEV);
  843. mnt = alloc_vfsmnt(name);
  844. if (!mnt)
  845. return ERR_PTR(-ENOMEM);
  846. if (flags & MS_KERNMOUNT)
  847. mnt->mnt.mnt_flags = MNT_INTERNAL;
  848. root = mount_fs(type, flags, name, data);
  849. if (IS_ERR(root)) {
  850. mnt_free_id(mnt);
  851. free_vfsmnt(mnt);
  852. return ERR_CAST(root);
  853. }
  854. mnt->mnt.mnt_root = root;
  855. mnt->mnt.mnt_sb = root->d_sb;
  856. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  857. mnt->mnt_parent = mnt;
  858. lock_mount_hash();
  859. list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
  860. unlock_mount_hash();
  861. return &mnt->mnt;
  862. }
  863. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  864. static struct mount *clone_mnt(struct mount *old, struct dentry *root,
  865. int flag)
  866. {
  867. struct super_block *sb = old->mnt.mnt_sb;
  868. struct mount *mnt;
  869. int err;
  870. mnt = alloc_vfsmnt(old->mnt_devname);
  871. if (!mnt)
  872. return ERR_PTR(-ENOMEM);
  873. if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
  874. mnt->mnt_group_id = 0; /* not a peer of original */
  875. else
  876. mnt->mnt_group_id = old->mnt_group_id;
  877. if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
  878. err = mnt_alloc_group_id(mnt);
  879. if (err)
  880. goto out_free;
  881. }
  882. mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED);
  883. /* Don't allow unprivileged users to change mount flags */
  884. if (flag & CL_UNPRIVILEGED) {
  885. mnt->mnt.mnt_flags |= MNT_LOCK_ATIME;
  886. if (mnt->mnt.mnt_flags & MNT_READONLY)
  887. mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
  888. if (mnt->mnt.mnt_flags & MNT_NODEV)
  889. mnt->mnt.mnt_flags |= MNT_LOCK_NODEV;
  890. if (mnt->mnt.mnt_flags & MNT_NOSUID)
  891. mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID;
  892. if (mnt->mnt.mnt_flags & MNT_NOEXEC)
  893. mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC;
  894. }
  895. /* Don't allow unprivileged users to reveal what is under a mount */
  896. if ((flag & CL_UNPRIVILEGED) &&
  897. (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire)))
  898. mnt->mnt.mnt_flags |= MNT_LOCKED;
  899. atomic_inc(&sb->s_active);
  900. mnt->mnt.mnt_sb = sb;
  901. mnt->mnt.mnt_root = dget(root);
  902. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  903. mnt->mnt_parent = mnt;
  904. lock_mount_hash();
  905. list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
  906. unlock_mount_hash();
  907. if ((flag & CL_SLAVE) ||
  908. ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
  909. list_add(&mnt->mnt_slave, &old->mnt_slave_list);
  910. mnt->mnt_master = old;
  911. CLEAR_MNT_SHARED(mnt);
  912. } else if (!(flag & CL_PRIVATE)) {
  913. if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
  914. list_add(&mnt->mnt_share, &old->mnt_share);
  915. if (IS_MNT_SLAVE(old))
  916. list_add(&mnt->mnt_slave, &old->mnt_slave);
  917. mnt->mnt_master = old->mnt_master;
  918. } else {
  919. CLEAR_MNT_SHARED(mnt);
  920. }
  921. if (flag & CL_MAKE_SHARED)
  922. set_mnt_shared(mnt);
  923. /* stick the duplicate mount on the same expiry list
  924. * as the original if that was on one */
  925. if (flag & CL_EXPIRE) {
  926. if (!list_empty(&old->mnt_expire))
  927. list_add(&mnt->mnt_expire, &old->mnt_expire);
  928. }
  929. return mnt;
  930. out_free:
  931. mnt_free_id(mnt);
  932. free_vfsmnt(mnt);
  933. return ERR_PTR(err);
  934. }
  935. static void cleanup_mnt(struct mount *mnt)
  936. {
  937. /*
  938. * This probably indicates that somebody messed
  939. * up a mnt_want/drop_write() pair. If this
  940. * happens, the filesystem was probably unable
  941. * to make r/w->r/o transitions.
  942. */
  943. /*
  944. * The locking used to deal with mnt_count decrement provides barriers,
  945. * so mnt_get_writers() below is safe.
  946. */
  947. WARN_ON(mnt_get_writers(mnt));
  948. if (unlikely(mnt->mnt_pins.first))
  949. mnt_pin_kill(mnt);
  950. fsnotify_vfsmount_delete(&mnt->mnt);
  951. dput(mnt->mnt.mnt_root);
  952. deactivate_super(mnt->mnt.mnt_sb);
  953. mnt_free_id(mnt);
  954. call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
  955. }
  956. static void __cleanup_mnt(struct rcu_head *head)
  957. {
  958. cleanup_mnt(container_of(head, struct mount, mnt_rcu));
  959. }
  960. static LLIST_HEAD(delayed_mntput_list);
  961. static void delayed_mntput(struct work_struct *unused)
  962. {
  963. struct llist_node *node = llist_del_all(&delayed_mntput_list);
  964. struct llist_node *next;
  965. for (; node; node = next) {
  966. next = llist_next(node);
  967. cleanup_mnt(llist_entry(node, struct mount, mnt_llist));
  968. }
  969. }
  970. static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
  971. static void mntput_no_expire(struct mount *mnt)
  972. {
  973. rcu_read_lock();
  974. mnt_add_count(mnt, -1);
  975. if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */
  976. rcu_read_unlock();
  977. return;
  978. }
  979. lock_mount_hash();
  980. if (mnt_get_count(mnt)) {
  981. rcu_read_unlock();
  982. unlock_mount_hash();
  983. return;
  984. }
  985. if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
  986. rcu_read_unlock();
  987. unlock_mount_hash();
  988. return;
  989. }
  990. mnt->mnt.mnt_flags |= MNT_DOOMED;
  991. rcu_read_unlock();
  992. list_del(&mnt->mnt_instance);
  993. if (unlikely(!list_empty(&mnt->mnt_mounts))) {
  994. struct mount *p, *tmp;
  995. list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
  996. umount_mnt(p);
  997. }
  998. }
  999. unlock_mount_hash();
  1000. if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
  1001. struct task_struct *task = current;
  1002. if (likely(!(task->flags & PF_KTHREAD))) {
  1003. init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
  1004. if (!task_work_add(task, &mnt->mnt_rcu, true))
  1005. return;
  1006. }
  1007. if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
  1008. schedule_delayed_work(&delayed_mntput_work, 1);
  1009. return;
  1010. }
  1011. cleanup_mnt(mnt);
  1012. }
  1013. void mntput(struct vfsmount *mnt)
  1014. {
  1015. if (mnt) {
  1016. struct mount *m = real_mount(mnt);
  1017. /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
  1018. if (unlikely(m->mnt_expiry_mark))
  1019. m->mnt_expiry_mark = 0;
  1020. mntput_no_expire(m);
  1021. }
  1022. }
  1023. EXPORT_SYMBOL(mntput);
  1024. struct vfsmount *mntget(struct vfsmount *mnt)
  1025. {
  1026. if (mnt)
  1027. mnt_add_count(real_mount(mnt), 1);
  1028. return mnt;
  1029. }
  1030. EXPORT_SYMBOL(mntget);
  1031. /* path_is_mountpoint() - Check if path is a mount in the current
  1032. * namespace.
  1033. *
  1034. * d_mountpoint() can only be used reliably to establish if a dentry is
  1035. * not mounted in any namespace and that common case is handled inline.
  1036. * d_mountpoint() isn't aware of the possibility there may be multiple
  1037. * mounts using a given dentry in a different namespace. This function
  1038. * checks if the passed in path is a mountpoint rather than the dentry
  1039. * alone.
  1040. */
  1041. bool path_is_mountpoint(const struct path *path)
  1042. {
  1043. unsigned seq;
  1044. bool res;
  1045. if (!d_mountpoint(path->dentry))
  1046. return false;
  1047. rcu_read_lock();
  1048. do {
  1049. seq = read_seqbegin(&mount_lock);
  1050. res = __path_is_mountpoint(path);
  1051. } while (read_seqretry(&mount_lock, seq));
  1052. rcu_read_unlock();
  1053. return res;
  1054. }
  1055. EXPORT_SYMBOL(path_is_mountpoint);
  1056. struct vfsmount *mnt_clone_internal(const struct path *path)
  1057. {
  1058. struct mount *p;
  1059. p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
  1060. if (IS_ERR(p))
  1061. return ERR_CAST(p);
  1062. p->mnt.mnt_flags |= MNT_INTERNAL;
  1063. return &p->mnt;
  1064. }
  1065. static inline void mangle(struct seq_file *m, const char *s)
  1066. {
  1067. seq_escape(m, s, " \t\n\\");
  1068. }
  1069. /*
  1070. * Simple .show_options callback for filesystems which don't want to
  1071. * implement more complex mount option showing.
  1072. *
  1073. * See also save_mount_options().
  1074. */
  1075. int generic_show_options(struct seq_file *m, struct dentry *root)
  1076. {
  1077. const char *options;
  1078. rcu_read_lock();
  1079. options = rcu_dereference(root->d_sb->s_options);
  1080. if (options != NULL && options[0]) {
  1081. seq_putc(m, ',');
  1082. mangle(m, options);
  1083. }
  1084. rcu_read_unlock();
  1085. return 0;
  1086. }
  1087. EXPORT_SYMBOL(generic_show_options);
  1088. /*
  1089. * If filesystem uses generic_show_options(), this function should be
  1090. * called from the fill_super() callback.
  1091. *
  1092. * The .remount_fs callback usually needs to be handled in a special
  1093. * way, to make sure, that previous options are not overwritten if the
  1094. * remount fails.
  1095. *
  1096. * Also note, that if the filesystem's .remount_fs function doesn't
  1097. * reset all options to their default value, but changes only newly
  1098. * given options, then the displayed options will not reflect reality
  1099. * any more.
  1100. */
  1101. void save_mount_options(struct super_block *sb, char *options)
  1102. {
  1103. BUG_ON(sb->s_options);
  1104. rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
  1105. }
  1106. EXPORT_SYMBOL(save_mount_options);
  1107. void replace_mount_options(struct super_block *sb, char *options)
  1108. {
  1109. char *old = sb->s_options;
  1110. rcu_assign_pointer(sb->s_options, options);
  1111. if (old) {
  1112. synchronize_rcu();
  1113. kfree(old);
  1114. }
  1115. }
  1116. EXPORT_SYMBOL(replace_mount_options);
  1117. #ifdef CONFIG_PROC_FS
  1118. /* iterator; we want it to have access to namespace_sem, thus here... */
  1119. static void *m_start(struct seq_file *m, loff_t *pos)
  1120. {
  1121. struct proc_mounts *p = m->private;
  1122. down_read(&namespace_sem);
  1123. if (p->cached_event == p->ns->event) {
  1124. void *v = p->cached_mount;
  1125. if (*pos == p->cached_index)
  1126. return v;
  1127. if (*pos == p->cached_index + 1) {
  1128. v = seq_list_next(v, &p->ns->list, &p->cached_index);
  1129. return p->cached_mount = v;
  1130. }
  1131. }
  1132. p->cached_event = p->ns->event;
  1133. p->cached_mount = seq_list_start(&p->ns->list, *pos);
  1134. p->cached_index = *pos;
  1135. return p->cached_mount;
  1136. }
  1137. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  1138. {
  1139. struct proc_mounts *p = m->private;
  1140. p->cached_mount = seq_list_next(v, &p->ns->list, pos);
  1141. p->cached_index = *pos;
  1142. return p->cached_mount;
  1143. }
  1144. static void m_stop(struct seq_file *m, void *v)
  1145. {
  1146. up_read(&namespace_sem);
  1147. }
  1148. static int m_show(struct seq_file *m, void *v)
  1149. {
  1150. struct proc_mounts *p = m->private;
  1151. struct mount *r = list_entry(v, struct mount, mnt_list);
  1152. return p->show(m, &r->mnt);
  1153. }
  1154. const struct seq_operations mounts_op = {
  1155. .start = m_start,
  1156. .next = m_next,
  1157. .stop = m_stop,
  1158. .show = m_show,
  1159. };
  1160. #endif /* CONFIG_PROC_FS */
  1161. /**
  1162. * may_umount_tree - check if a mount tree is busy
  1163. * @mnt: root of mount tree
  1164. *
  1165. * This is called to check if a tree of mounts has any
  1166. * open files, pwds, chroots or sub mounts that are
  1167. * busy.
  1168. */
  1169. int may_umount_tree(struct vfsmount *m)
  1170. {
  1171. struct mount *mnt = real_mount(m);
  1172. int actual_refs = 0;
  1173. int minimum_refs = 0;
  1174. struct mount *p;
  1175. BUG_ON(!m);
  1176. /* write lock needed for mnt_get_count */
  1177. lock_mount_hash();
  1178. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1179. actual_refs += mnt_get_count(p);
  1180. minimum_refs += 2;
  1181. }
  1182. unlock_mount_hash();
  1183. if (actual_refs > minimum_refs)
  1184. return 0;
  1185. return 1;
  1186. }
  1187. EXPORT_SYMBOL(may_umount_tree);
  1188. /**
  1189. * may_umount - check if a mount point is busy
  1190. * @mnt: root of mount
  1191. *
  1192. * This is called to check if a mount point has any
  1193. * open files, pwds, chroots or sub mounts. If the
  1194. * mount has sub mounts this will return busy
  1195. * regardless of whether the sub mounts are busy.
  1196. *
  1197. * Doesn't take quota and stuff into account. IOW, in some cases it will
  1198. * give false negatives. The main reason why it's here is that we need
  1199. * a non-destructive way to look for easily umountable filesystems.
  1200. */
  1201. int may_umount(struct vfsmount *mnt)
  1202. {
  1203. int ret = 1;
  1204. down_read(&namespace_sem);
  1205. lock_mount_hash();
  1206. if (propagate_mount_busy(real_mount(mnt), 2))
  1207. ret = 0;
  1208. unlock_mount_hash();
  1209. up_read(&namespace_sem);
  1210. return ret;
  1211. }
  1212. EXPORT_SYMBOL(may_umount);
  1213. static HLIST_HEAD(unmounted); /* protected by namespace_sem */
  1214. static void namespace_unlock(void)
  1215. {
  1216. struct hlist_head head;
  1217. hlist_move_list(&unmounted, &head);
  1218. up_write(&namespace_sem);
  1219. if (likely(hlist_empty(&head)))
  1220. return;
  1221. synchronize_rcu();
  1222. group_pin_kill(&head);
  1223. }
  1224. static inline void namespace_lock(void)
  1225. {
  1226. down_write(&namespace_sem);
  1227. }
  1228. enum umount_tree_flags {
  1229. UMOUNT_SYNC = 1,
  1230. UMOUNT_PROPAGATE = 2,
  1231. UMOUNT_CONNECTED = 4,
  1232. };
  1233. static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
  1234. {
  1235. /* Leaving mounts connected is only valid for lazy umounts */
  1236. if (how & UMOUNT_SYNC)
  1237. return true;
  1238. /* A mount without a parent has nothing to be connected to */
  1239. if (!mnt_has_parent(mnt))
  1240. return true;
  1241. /* Because the reference counting rules change when mounts are
  1242. * unmounted and connected, umounted mounts may not be
  1243. * connected to mounted mounts.
  1244. */
  1245. if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
  1246. return true;
  1247. /* Has it been requested that the mount remain connected? */
  1248. if (how & UMOUNT_CONNECTED)
  1249. return false;
  1250. /* Is the mount locked such that it needs to remain connected? */
  1251. if (IS_MNT_LOCKED(mnt))
  1252. return false;
  1253. /* By default disconnect the mount */
  1254. return true;
  1255. }
  1256. /*
  1257. * mount_lock must be held
  1258. * namespace_sem must be held for write
  1259. */
  1260. static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
  1261. {
  1262. LIST_HEAD(tmp_list);
  1263. struct mount *p;
  1264. if (how & UMOUNT_PROPAGATE)
  1265. propagate_mount_unlock(mnt);
  1266. /* Gather the mounts to umount */
  1267. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1268. p->mnt.mnt_flags |= MNT_UMOUNT;
  1269. list_move(&p->mnt_list, &tmp_list);
  1270. }
  1271. /* Hide the mounts from mnt_mounts */
  1272. list_for_each_entry(p, &tmp_list, mnt_list) {
  1273. list_del_init(&p->mnt_child);
  1274. }
  1275. /* Add propogated mounts to the tmp_list */
  1276. if (how & UMOUNT_PROPAGATE)
  1277. propagate_umount(&tmp_list);
  1278. while (!list_empty(&tmp_list)) {
  1279. struct mnt_namespace *ns;
  1280. bool disconnect;
  1281. p = list_first_entry(&tmp_list, struct mount, mnt_list);
  1282. list_del_init(&p->mnt_expire);
  1283. list_del_init(&p->mnt_list);
  1284. ns = p->mnt_ns;
  1285. if (ns) {
  1286. ns->mounts--;
  1287. __touch_mnt_namespace(ns);
  1288. }
  1289. p->mnt_ns = NULL;
  1290. if (how & UMOUNT_SYNC)
  1291. p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
  1292. disconnect = disconnect_mount(p, how);
  1293. pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt,
  1294. disconnect ? &unmounted : NULL);
  1295. if (mnt_has_parent(p)) {
  1296. mnt_add_count(p->mnt_parent, -1);
  1297. if (!disconnect) {
  1298. /* Don't forget about p */
  1299. list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
  1300. } else {
  1301. umount_mnt(p);
  1302. }
  1303. }
  1304. change_mnt_propagation(p, MS_PRIVATE);
  1305. }
  1306. }
  1307. static void shrink_submounts(struct mount *mnt);
  1308. static int do_umount(struct mount *mnt, int flags)
  1309. {
  1310. struct super_block *sb = mnt->mnt.mnt_sb;
  1311. int retval;
  1312. retval = security_sb_umount(&mnt->mnt, flags);
  1313. if (retval)
  1314. return retval;
  1315. /*
  1316. * Allow userspace to request a mountpoint be expired rather than
  1317. * unmounting unconditionally. Unmount only happens if:
  1318. * (1) the mark is already set (the mark is cleared by mntput())
  1319. * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
  1320. */
  1321. if (flags & MNT_EXPIRE) {
  1322. if (&mnt->mnt == current->fs->root.mnt ||
  1323. flags & (MNT_FORCE | MNT_DETACH))
  1324. return -EINVAL;
  1325. /*
  1326. * probably don't strictly need the lock here if we examined
  1327. * all race cases, but it's a slowpath.
  1328. */
  1329. lock_mount_hash();
  1330. if (mnt_get_count(mnt) != 2) {
  1331. unlock_mount_hash();
  1332. return -EBUSY;
  1333. }
  1334. unlock_mount_hash();
  1335. if (!xchg(&mnt->mnt_expiry_mark, 1))
  1336. return -EAGAIN;
  1337. }
  1338. /*
  1339. * If we may have to abort operations to get out of this
  1340. * mount, and they will themselves hold resources we must
  1341. * allow the fs to do things. In the Unix tradition of
  1342. * 'Gee thats tricky lets do it in userspace' the umount_begin
  1343. * might fail to complete on the first run through as other tasks
  1344. * must return, and the like. Thats for the mount program to worry
  1345. * about for the moment.
  1346. */
  1347. if (flags & MNT_FORCE && sb->s_op->umount_begin) {
  1348. sb->s_op->umount_begin(sb);
  1349. }
  1350. /*
  1351. * No sense to grab the lock for this test, but test itself looks
  1352. * somewhat bogus. Suggestions for better replacement?
  1353. * Ho-hum... In principle, we might treat that as umount + switch
  1354. * to rootfs. GC would eventually take care of the old vfsmount.
  1355. * Actually it makes sense, especially if rootfs would contain a
  1356. * /reboot - static binary that would close all descriptors and
  1357. * call reboot(9). Then init(8) could umount root and exec /reboot.
  1358. */
  1359. if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
  1360. /*
  1361. * Special case for "unmounting" root ...
  1362. * we just try to remount it readonly.
  1363. */
  1364. if (!capable(CAP_SYS_ADMIN))
  1365. return -EPERM;
  1366. down_write(&sb->s_umount);
  1367. if (!(sb->s_flags & MS_RDONLY))
  1368. retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
  1369. up_write(&sb->s_umount);
  1370. return retval;
  1371. }
  1372. namespace_lock();
  1373. lock_mount_hash();
  1374. event++;
  1375. if (flags & MNT_DETACH) {
  1376. if (!list_empty(&mnt->mnt_list))
  1377. umount_tree(mnt, UMOUNT_PROPAGATE);
  1378. retval = 0;
  1379. } else {
  1380. shrink_submounts(mnt);
  1381. retval = -EBUSY;
  1382. if (!propagate_mount_busy(mnt, 2)) {
  1383. if (!list_empty(&mnt->mnt_list))
  1384. umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
  1385. retval = 0;
  1386. }
  1387. }
  1388. unlock_mount_hash();
  1389. namespace_unlock();
  1390. return retval;
  1391. }
  1392. /*
  1393. * __detach_mounts - lazily unmount all mounts on the specified dentry
  1394. *
  1395. * During unlink, rmdir, and d_drop it is possible to loose the path
  1396. * to an existing mountpoint, and wind up leaking the mount.
  1397. * detach_mounts allows lazily unmounting those mounts instead of
  1398. * leaking them.
  1399. *
  1400. * The caller may hold dentry->d_inode->i_mutex.
  1401. */
  1402. void __detach_mounts(struct dentry *dentry)
  1403. {
  1404. struct mountpoint *mp;
  1405. struct mount *mnt;
  1406. namespace_lock();
  1407. mp = lookup_mountpoint(dentry);
  1408. if (IS_ERR_OR_NULL(mp))
  1409. goto out_unlock;
  1410. lock_mount_hash();
  1411. event++;
  1412. while (!hlist_empty(&mp->m_list)) {
  1413. mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
  1414. if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
  1415. hlist_add_head(&mnt->mnt_umount.s_list, &unmounted);
  1416. umount_mnt(mnt);
  1417. }
  1418. else umount_tree(mnt, UMOUNT_CONNECTED);
  1419. }
  1420. unlock_mount_hash();
  1421. put_mountpoint(mp);
  1422. out_unlock:
  1423. namespace_unlock();
  1424. }
  1425. /*
  1426. * Is the caller allowed to modify his namespace?
  1427. */
  1428. static inline bool may_mount(void)
  1429. {
  1430. return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
  1431. }
  1432. static inline bool may_mandlock(void)
  1433. {
  1434. #ifndef CONFIG_MANDATORY_FILE_LOCKING
  1435. return false;
  1436. #endif
  1437. return capable(CAP_SYS_ADMIN);
  1438. }
  1439. /*
  1440. * Now umount can handle mount points as well as block devices.
  1441. * This is important for filesystems which use unnamed block devices.
  1442. *
  1443. * We now support a flag for forced unmount like the other 'big iron'
  1444. * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
  1445. */
  1446. SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
  1447. {
  1448. struct path path;
  1449. struct mount *mnt;
  1450. int retval;
  1451. int lookup_flags = 0;
  1452. if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
  1453. return -EINVAL;
  1454. if (!may_mount())
  1455. return -EPERM;
  1456. if (!(flags & UMOUNT_NOFOLLOW))
  1457. lookup_flags |= LOOKUP_FOLLOW;
  1458. retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
  1459. if (retval)
  1460. goto out;
  1461. mnt = real_mount(path.mnt);
  1462. retval = -EINVAL;
  1463. if (path.dentry != path.mnt->mnt_root)
  1464. goto dput_and_out;
  1465. if (!check_mnt(mnt))
  1466. goto dput_and_out;
  1467. if (mnt->mnt.mnt_flags & MNT_LOCKED)
  1468. goto dput_and_out;
  1469. retval = -EPERM;
  1470. if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
  1471. goto dput_and_out;
  1472. retval = do_umount(mnt, flags);
  1473. dput_and_out:
  1474. /* we mustn't call path_put() as that would clear mnt_expiry_mark */
  1475. dput(path.dentry);
  1476. mntput_no_expire(mnt);
  1477. out:
  1478. return retval;
  1479. }
  1480. #ifdef __ARCH_WANT_SYS_OLDUMOUNT
  1481. /*
  1482. * The 2.0 compatible umount. No flags.
  1483. */
  1484. SYSCALL_DEFINE1(oldumount, char __user *, name)
  1485. {
  1486. return sys_umount(name, 0);
  1487. }
  1488. #endif
  1489. static bool is_mnt_ns_file(struct dentry *dentry)
  1490. {
  1491. /* Is this a proxy for a mount namespace? */
  1492. return dentry->d_op == &ns_dentry_operations &&
  1493. dentry->d_fsdata == &mntns_operations;
  1494. }
  1495. struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
  1496. {
  1497. return container_of(ns, struct mnt_namespace, ns);
  1498. }
  1499. static bool mnt_ns_loop(struct dentry *dentry)
  1500. {
  1501. /* Could bind mounting the mount namespace inode cause a
  1502. * mount namespace loop?
  1503. */
  1504. struct mnt_namespace *mnt_ns;
  1505. if (!is_mnt_ns_file(dentry))
  1506. return false;
  1507. mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
  1508. return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
  1509. }
  1510. struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
  1511. int flag)
  1512. {
  1513. struct mount *res, *p, *q, *r, *parent;
  1514. if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
  1515. return ERR_PTR(-EINVAL);
  1516. if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
  1517. return ERR_PTR(-EINVAL);
  1518. res = q = clone_mnt(mnt, dentry, flag);
  1519. if (IS_ERR(q))
  1520. return q;
  1521. q->mnt_mountpoint = mnt->mnt_mountpoint;
  1522. p = mnt;
  1523. list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
  1524. struct mount *s;
  1525. if (!is_subdir(r->mnt_mountpoint, dentry))
  1526. continue;
  1527. for (s = r; s; s = next_mnt(s, r)) {
  1528. struct mount *t = NULL;
  1529. if (!(flag & CL_COPY_UNBINDABLE) &&
  1530. IS_MNT_UNBINDABLE(s)) {
  1531. s = skip_mnt_tree(s);
  1532. continue;
  1533. }
  1534. if (!(flag & CL_COPY_MNT_NS_FILE) &&
  1535. is_mnt_ns_file(s->mnt.mnt_root)) {
  1536. s = skip_mnt_tree(s);
  1537. continue;
  1538. }
  1539. while (p != s->mnt_parent) {
  1540. p = p->mnt_parent;
  1541. q = q->mnt_parent;
  1542. }
  1543. p = s;
  1544. parent = q;
  1545. q = clone_mnt(p, p->mnt.mnt_root, flag);
  1546. if (IS_ERR(q))
  1547. goto out;
  1548. lock_mount_hash();
  1549. list_add_tail(&q->mnt_list, &res->mnt_list);
  1550. mnt_set_mountpoint(parent, p->mnt_mp, q);
  1551. if (!list_empty(&parent->mnt_mounts)) {
  1552. t = list_last_entry(&parent->mnt_mounts,
  1553. struct mount, mnt_child);
  1554. if (t->mnt_mp != p->mnt_mp)
  1555. t = NULL;
  1556. }
  1557. attach_shadowed(q, parent, t);
  1558. unlock_mount_hash();
  1559. }
  1560. }
  1561. return res;
  1562. out:
  1563. if (res) {
  1564. lock_mount_hash();
  1565. umount_tree(res, UMOUNT_SYNC);
  1566. unlock_mount_hash();
  1567. }
  1568. return q;
  1569. }
  1570. /* Caller should check returned pointer for errors */
  1571. struct vfsmount *collect_mounts(const struct path *path)
  1572. {
  1573. struct mount *tree;
  1574. namespace_lock();
  1575. if (!check_mnt(real_mount(path->mnt)))
  1576. tree = ERR_PTR(-EINVAL);
  1577. else
  1578. tree = copy_tree(real_mount(path->mnt), path->dentry,
  1579. CL_COPY_ALL | CL_PRIVATE);
  1580. namespace_unlock();
  1581. if (IS_ERR(tree))
  1582. return ERR_CAST(tree);
  1583. return &tree->mnt;
  1584. }
  1585. void drop_collected_mounts(struct vfsmount *mnt)
  1586. {
  1587. namespace_lock();
  1588. lock_mount_hash();
  1589. umount_tree(real_mount(mnt), UMOUNT_SYNC);
  1590. unlock_mount_hash();
  1591. namespace_unlock();
  1592. }
  1593. /**
  1594. * clone_private_mount - create a private clone of a path
  1595. *
  1596. * This creates a new vfsmount, which will be the clone of @path. The new will
  1597. * not be attached anywhere in the namespace and will be private (i.e. changes
  1598. * to the originating mount won't be propagated into this).
  1599. *
  1600. * Release with mntput().
  1601. */
  1602. struct vfsmount *clone_private_mount(const struct path *path)
  1603. {
  1604. struct mount *old_mnt = real_mount(path->mnt);
  1605. struct mount *new_mnt;
  1606. if (IS_MNT_UNBINDABLE(old_mnt))
  1607. return ERR_PTR(-EINVAL);
  1608. new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
  1609. if (IS_ERR(new_mnt))
  1610. return ERR_CAST(new_mnt);
  1611. return &new_mnt->mnt;
  1612. }
  1613. EXPORT_SYMBOL_GPL(clone_private_mount);
  1614. int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
  1615. struct vfsmount *root)
  1616. {
  1617. struct mount *mnt;
  1618. int res = f(root, arg);
  1619. if (res)
  1620. return res;
  1621. list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
  1622. res = f(&mnt->mnt, arg);
  1623. if (res)
  1624. return res;
  1625. }
  1626. return 0;
  1627. }
  1628. static void cleanup_group_ids(struct mount *mnt, struct mount *end)
  1629. {
  1630. struct mount *p;
  1631. for (p = mnt; p != end; p = next_mnt(p, mnt)) {
  1632. if (p->mnt_group_id && !IS_MNT_SHARED(p))
  1633. mnt_release_group_id(p);
  1634. }
  1635. }
  1636. static int invent_group_ids(struct mount *mnt, bool recurse)
  1637. {
  1638. struct mount *p;
  1639. for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
  1640. if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
  1641. int err = mnt_alloc_group_id(p);
  1642. if (err) {
  1643. cleanup_group_ids(mnt, p);
  1644. return err;
  1645. }
  1646. }
  1647. }
  1648. return 0;
  1649. }
  1650. int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
  1651. {
  1652. unsigned int max = READ_ONCE(sysctl_mount_max);
  1653. unsigned int mounts = 0, old, pending, sum;
  1654. struct mount *p;
  1655. for (p = mnt; p; p = next_mnt(p, mnt))
  1656. mounts++;
  1657. old = ns->mounts;
  1658. pending = ns->pending_mounts;
  1659. sum = old + pending;
  1660. if ((old > sum) ||
  1661. (pending > sum) ||
  1662. (max < sum) ||
  1663. (mounts > (max - sum)))
  1664. return -ENOSPC;
  1665. ns->pending_mounts = pending + mounts;
  1666. return 0;
  1667. }
  1668. /*
  1669. * @source_mnt : mount tree to be attached
  1670. * @nd : place the mount tree @source_mnt is attached
  1671. * @parent_nd : if non-null, detach the source_mnt from its parent and
  1672. * store the parent mount and mountpoint dentry.
  1673. * (done when source_mnt is moved)
  1674. *
  1675. * NOTE: in the table below explains the semantics when a source mount
  1676. * of a given type is attached to a destination mount of a given type.
  1677. * ---------------------------------------------------------------------------
  1678. * | BIND MOUNT OPERATION |
  1679. * |**************************************************************************
  1680. * | source-->| shared | private | slave | unbindable |
  1681. * | dest | | | | |
  1682. * | | | | | | |
  1683. * | v | | | | |
  1684. * |**************************************************************************
  1685. * | shared | shared (++) | shared (+) | shared(+++)| invalid |
  1686. * | | | | | |
  1687. * |non-shared| shared (+) | private | slave (*) | invalid |
  1688. * ***************************************************************************
  1689. * A bind operation clones the source mount and mounts the clone on the
  1690. * destination mount.
  1691. *
  1692. * (++) the cloned mount is propagated to all the mounts in the propagation
  1693. * tree of the destination mount and the cloned mount is added to
  1694. * the peer group of the source mount.
  1695. * (+) the cloned mount is created under the destination mount and is marked
  1696. * as shared. The cloned mount is added to the peer group of the source
  1697. * mount.
  1698. * (+++) the mount is propagated to all the mounts in the propagation tree
  1699. * of the destination mount and the cloned mount is made slave
  1700. * of the same master as that of the source mount. The cloned mount
  1701. * is marked as 'shared and slave'.
  1702. * (*) the cloned mount is made a slave of the same master as that of the
  1703. * source mount.
  1704. *
  1705. * ---------------------------------------------------------------------------
  1706. * | MOVE MOUNT OPERATION |
  1707. * |**************************************************************************
  1708. * | source-->| shared | private | slave | unbindable |
  1709. * | dest | | | | |
  1710. * | | | | | | |
  1711. * | v | | | | |
  1712. * |**************************************************************************
  1713. * | shared | shared (+) | shared (+) | shared(+++) | invalid |
  1714. * | | | | | |
  1715. * |non-shared| shared (+*) | private | slave (*) | unbindable |
  1716. * ***************************************************************************
  1717. *
  1718. * (+) the mount is moved to the destination. And is then propagated to
  1719. * all the mounts in the propagation tree of the destination mount.
  1720. * (+*) the mount is moved to the destination.
  1721. * (+++) the mount is moved to the destination and is then propagated to
  1722. * all the mounts belonging to the destination mount's propagation tree.
  1723. * the mount is marked as 'shared and slave'.
  1724. * (*) the mount continues to be a slave at the new location.
  1725. *
  1726. * if the source mount is a tree, the operations explained above is
  1727. * applied to each mount in the tree.
  1728. * Must be called without spinlocks held, since this function can sleep
  1729. * in allocations.
  1730. */
  1731. static int attach_recursive_mnt(struct mount *source_mnt,
  1732. struct mount *dest_mnt,
  1733. struct mountpoint *dest_mp,
  1734. struct path *parent_path)
  1735. {
  1736. HLIST_HEAD(tree_list);
  1737. struct mnt_namespace *ns = dest_mnt->mnt_ns;
  1738. struct mount *child, *p;
  1739. struct hlist_node *n;
  1740. int err;
  1741. /* Is there space to add these mounts to the mount namespace? */
  1742. if (!parent_path) {
  1743. err = count_mounts(ns, source_mnt);
  1744. if (err)
  1745. goto out;
  1746. }
  1747. if (IS_MNT_SHARED(dest_mnt)) {
  1748. err = invent_group_ids(source_mnt, true);
  1749. if (err)
  1750. goto out;
  1751. err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
  1752. lock_mount_hash();
  1753. if (err)
  1754. goto out_cleanup_ids;
  1755. for (p = source_mnt; p; p = next_mnt(p, source_mnt))
  1756. set_mnt_shared(p);
  1757. } else {
  1758. lock_mount_hash();
  1759. }
  1760. if (parent_path) {
  1761. detach_mnt(source_mnt, parent_path);
  1762. attach_mnt(source_mnt, dest_mnt, dest_mp);
  1763. touch_mnt_namespace(source_mnt->mnt_ns);
  1764. } else {
  1765. mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
  1766. commit_tree(source_mnt, NULL);
  1767. }
  1768. hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
  1769. struct mount *q;
  1770. hlist_del_init(&child->mnt_hash);
  1771. q = __lookup_mnt_last(&child->mnt_parent->mnt,
  1772. child->mnt_mountpoint);
  1773. commit_tree(child, q);
  1774. }
  1775. unlock_mount_hash();
  1776. return 0;
  1777. out_cleanup_ids:
  1778. while (!hlist_empty(&tree_list)) {
  1779. child = hlist_entry(tree_list.first, struct mount, mnt_hash);
  1780. child->mnt_parent->mnt_ns->pending_mounts = 0;
  1781. umount_tree(child, UMOUNT_SYNC);
  1782. }
  1783. unlock_mount_hash();
  1784. cleanup_group_ids(source_mnt, NULL);
  1785. out:
  1786. ns->pending_mounts = 0;
  1787. return err;
  1788. }
  1789. static struct mountpoint *lock_mount(struct path *path)
  1790. {
  1791. struct vfsmount *mnt;
  1792. struct dentry *dentry = path->dentry;
  1793. retry:
  1794. inode_lock(dentry->d_inode);
  1795. if (unlikely(cant_mount(dentry))) {
  1796. inode_unlock(dentry->d_inode);
  1797. return ERR_PTR(-ENOENT);
  1798. }
  1799. namespace_lock();
  1800. mnt = lookup_mnt(path);
  1801. if (likely(!mnt)) {
  1802. struct mountpoint *mp = lookup_mountpoint(dentry);
  1803. if (!mp)
  1804. mp = new_mountpoint(dentry);
  1805. if (IS_ERR(mp)) {
  1806. namespace_unlock();
  1807. inode_unlock(dentry->d_inode);
  1808. return mp;
  1809. }
  1810. return mp;
  1811. }
  1812. namespace_unlock();
  1813. inode_unlock(path->dentry->d_inode);
  1814. path_put(path);
  1815. path->mnt = mnt;
  1816. dentry = path->dentry = dget(mnt->mnt_root);
  1817. goto retry;
  1818. }
  1819. static void unlock_mount(struct mountpoint *where)
  1820. {
  1821. struct dentry *dentry = where->m_dentry;
  1822. put_mountpoint(where);
  1823. namespace_unlock();
  1824. inode_unlock(dentry->d_inode);
  1825. }
  1826. static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
  1827. {
  1828. if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
  1829. return -EINVAL;
  1830. if (d_is_dir(mp->m_dentry) !=
  1831. d_is_dir(mnt->mnt.mnt_root))
  1832. return -ENOTDIR;
  1833. return attach_recursive_mnt(mnt, p, mp, NULL);
  1834. }
  1835. /*
  1836. * Sanity check the flags to change_mnt_propagation.
  1837. */
  1838. static int flags_to_propagation_type(int flags)
  1839. {
  1840. int type = flags & ~(MS_REC | MS_SILENT);
  1841. /* Fail if any non-propagation flags are set */
  1842. if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1843. return 0;
  1844. /* Only one propagation flag should be set */
  1845. if (!is_power_of_2(type))
  1846. return 0;
  1847. return type;
  1848. }
  1849. /*
  1850. * recursively change the type of the mountpoint.
  1851. */
  1852. static int do_change_type(struct path *path, int flag)
  1853. {
  1854. struct mount *m;
  1855. struct mount *mnt = real_mount(path->mnt);
  1856. int recurse = flag & MS_REC;
  1857. int type;
  1858. int err = 0;
  1859. if (path->dentry != path->mnt->mnt_root)
  1860. return -EINVAL;
  1861. type = flags_to_propagation_type(flag);
  1862. if (!type)
  1863. return -EINVAL;
  1864. namespace_lock();
  1865. if (type == MS_SHARED) {
  1866. err = invent_group_ids(mnt, recurse);
  1867. if (err)
  1868. goto out_unlock;
  1869. }
  1870. lock_mount_hash();
  1871. for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
  1872. change_mnt_propagation(m, type);
  1873. unlock_mount_hash();
  1874. out_unlock:
  1875. namespace_unlock();
  1876. return err;
  1877. }
  1878. static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
  1879. {
  1880. struct mount *child;
  1881. list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
  1882. if (!is_subdir(child->mnt_mountpoint, dentry))
  1883. continue;
  1884. if (child->mnt.mnt_flags & MNT_LOCKED)
  1885. return true;
  1886. }
  1887. return false;
  1888. }
  1889. /*
  1890. * do loopback mount.
  1891. */
  1892. static int do_loopback(struct path *path, const char *old_name,
  1893. int recurse)
  1894. {
  1895. struct path old_path;
  1896. struct mount *mnt = NULL, *old, *parent;
  1897. struct mountpoint *mp;
  1898. int err;
  1899. if (!old_name || !*old_name)
  1900. return -EINVAL;
  1901. err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
  1902. if (err)
  1903. return err;
  1904. err = -EINVAL;
  1905. if (mnt_ns_loop(old_path.dentry))
  1906. goto out;
  1907. mp = lock_mount(path);
  1908. err = PTR_ERR(mp);
  1909. if (IS_ERR(mp))
  1910. goto out;
  1911. old = real_mount(old_path.mnt);
  1912. parent = real_mount(path->mnt);
  1913. err = -EINVAL;
  1914. if (IS_MNT_UNBINDABLE(old))
  1915. goto out2;
  1916. if (!check_mnt(parent))
  1917. goto out2;
  1918. if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations)
  1919. goto out2;
  1920. if (!recurse && has_locked_children(old, old_path.dentry))
  1921. goto out2;
  1922. if (recurse)
  1923. mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
  1924. else
  1925. mnt = clone_mnt(old, old_path.dentry, 0);
  1926. if (IS_ERR(mnt)) {
  1927. err = PTR_ERR(mnt);
  1928. goto out2;
  1929. }
  1930. mnt->mnt.mnt_flags &= ~MNT_LOCKED;
  1931. err = graft_tree(mnt, parent, mp);
  1932. if (err) {
  1933. lock_mount_hash();
  1934. umount_tree(mnt, UMOUNT_SYNC);
  1935. unlock_mount_hash();
  1936. }
  1937. out2:
  1938. unlock_mount(mp);
  1939. out:
  1940. path_put(&old_path);
  1941. return err;
  1942. }
  1943. static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
  1944. {
  1945. int error = 0;
  1946. int readonly_request = 0;
  1947. if (ms_flags & MS_RDONLY)
  1948. readonly_request = 1;
  1949. if (readonly_request == __mnt_is_readonly(mnt))
  1950. return 0;
  1951. if (readonly_request)
  1952. error = mnt_make_readonly(real_mount(mnt));
  1953. else
  1954. __mnt_unmake_readonly(real_mount(mnt));
  1955. return error;
  1956. }
  1957. /*
  1958. * change filesystem flags. dir should be a physical root of filesystem.
  1959. * If you've mounted a non-root directory somewhere and want to do remount
  1960. * on it - tough luck.
  1961. */
  1962. static int do_remount(struct path *path, int flags, int mnt_flags,
  1963. void *data)
  1964. {
  1965. int err;
  1966. struct super_block *sb = path->mnt->mnt_sb;
  1967. struct mount *mnt = real_mount(path->mnt);
  1968. if (!check_mnt(mnt))
  1969. return -EINVAL;
  1970. if (path->dentry != path->mnt->mnt_root)
  1971. return -EINVAL;
  1972. /* Don't allow changing of locked mnt flags.
  1973. *
  1974. * No locks need to be held here while testing the various
  1975. * MNT_LOCK flags because those flags can never be cleared
  1976. * once they are set.
  1977. */
  1978. if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
  1979. !(mnt_flags & MNT_READONLY)) {
  1980. return -EPERM;
  1981. }
  1982. if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
  1983. !(mnt_flags & MNT_NODEV)) {
  1984. return -EPERM;
  1985. }
  1986. if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
  1987. !(mnt_flags & MNT_NOSUID)) {
  1988. return -EPERM;
  1989. }
  1990. if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
  1991. !(mnt_flags & MNT_NOEXEC)) {
  1992. return -EPERM;
  1993. }
  1994. if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
  1995. ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
  1996. return -EPERM;
  1997. }
  1998. err = security_sb_remount(sb, data);
  1999. if (err)
  2000. return err;
  2001. down_write(&sb->s_umount);
  2002. if (flags & MS_BIND)
  2003. err = change_mount_flags(path->mnt, flags);
  2004. else if (!capable(CAP_SYS_ADMIN))
  2005. err = -EPERM;
  2006. else
  2007. err = do_remount_sb(sb, flags, data, 0);
  2008. if (!err) {
  2009. lock_mount_hash();
  2010. mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
  2011. mnt->mnt.mnt_flags = mnt_flags;
  2012. touch_mnt_namespace(mnt->mnt_ns);
  2013. unlock_mount_hash();
  2014. }
  2015. up_write(&sb->s_umount);
  2016. return err;
  2017. }
  2018. static inline int tree_contains_unbindable(struct mount *mnt)
  2019. {
  2020. struct mount *p;
  2021. for (p = mnt; p; p = next_mnt(p, mnt)) {
  2022. if (IS_MNT_UNBINDABLE(p))
  2023. return 1;
  2024. }
  2025. return 0;
  2026. }
  2027. static int do_move_mount(struct path *path, const char *old_name)
  2028. {
  2029. struct path old_path, parent_path;
  2030. struct mount *p;
  2031. struct mount *old;
  2032. struct mountpoint *mp;
  2033. int err;
  2034. if (!old_name || !*old_name)
  2035. return -EINVAL;
  2036. err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
  2037. if (err)
  2038. return err;
  2039. mp = lock_mount(path);
  2040. err = PTR_ERR(mp);
  2041. if (IS_ERR(mp))
  2042. goto out;
  2043. old = real_mount(old_path.mnt);
  2044. p = real_mount(path->mnt);
  2045. err = -EINVAL;
  2046. if (!check_mnt(p) || !check_mnt(old))
  2047. goto out1;
  2048. if (old->mnt.mnt_flags & MNT_LOCKED)
  2049. goto out1;
  2050. err = -EINVAL;
  2051. if (old_path.dentry != old_path.mnt->mnt_root)
  2052. goto out1;
  2053. if (!mnt_has_parent(old))
  2054. goto out1;
  2055. if (d_is_dir(path->dentry) !=
  2056. d_is_dir(old_path.dentry))
  2057. goto out1;
  2058. /*
  2059. * Don't move a mount residing in a shared parent.
  2060. */
  2061. if (IS_MNT_SHARED(old->mnt_parent))
  2062. goto out1;
  2063. /*
  2064. * Don't move a mount tree containing unbindable mounts to a destination
  2065. * mount which is shared.
  2066. */
  2067. if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
  2068. goto out1;
  2069. err = -ELOOP;
  2070. for (; mnt_has_parent(p); p = p->mnt_parent)
  2071. if (p == old)
  2072. goto out1;
  2073. err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
  2074. if (err)
  2075. goto out1;
  2076. /* if the mount is moved, it should no longer be expire
  2077. * automatically */
  2078. list_del_init(&old->mnt_expire);
  2079. out1:
  2080. unlock_mount(mp);
  2081. out:
  2082. if (!err)
  2083. path_put(&parent_path);
  2084. path_put(&old_path);
  2085. return err;
  2086. }
  2087. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  2088. {
  2089. int err;
  2090. const char *subtype = strchr(fstype, '.');
  2091. if (subtype) {
  2092. subtype++;
  2093. err = -EINVAL;
  2094. if (!subtype[0])
  2095. goto err;
  2096. } else
  2097. subtype = "";
  2098. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  2099. err = -ENOMEM;
  2100. if (!mnt->mnt_sb->s_subtype)
  2101. goto err;
  2102. return mnt;
  2103. err:
  2104. mntput(mnt);
  2105. return ERR_PTR(err);
  2106. }
  2107. /*
  2108. * add a mount into a namespace's mount tree
  2109. */
  2110. static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
  2111. {
  2112. struct mountpoint *mp;
  2113. struct mount *parent;
  2114. int err;
  2115. mnt_flags &= ~MNT_INTERNAL_FLAGS;
  2116. mp = lock_mount(path);
  2117. if (IS_ERR(mp))
  2118. return PTR_ERR(mp);
  2119. parent = real_mount(path->mnt);
  2120. err = -EINVAL;
  2121. if (unlikely(!check_mnt(parent))) {
  2122. /* that's acceptable only for automounts done in private ns */
  2123. if (!(mnt_flags & MNT_SHRINKABLE))
  2124. goto unlock;
  2125. /* ... and for those we'd better have mountpoint still alive */
  2126. if (!parent->mnt_ns)
  2127. goto unlock;
  2128. }
  2129. /* Refuse the same filesystem on the same mount point */
  2130. err = -EBUSY;
  2131. if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
  2132. path->mnt->mnt_root == path->dentry)
  2133. goto unlock;
  2134. err = -EINVAL;
  2135. if (d_is_symlink(newmnt->mnt.mnt_root))
  2136. goto unlock;
  2137. newmnt->mnt.mnt_flags = mnt_flags;
  2138. err = graft_tree(newmnt, parent, mp);
  2139. unlock:
  2140. unlock_mount(mp);
  2141. return err;
  2142. }
  2143. static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags);
  2144. /*
  2145. * create a new mount for userspace and request it to be added into the
  2146. * namespace's tree
  2147. */
  2148. static int do_new_mount(struct path *path, const char *fstype, int flags,
  2149. int mnt_flags, const char *name, void *data)
  2150. {
  2151. struct file_system_type *type;
  2152. struct vfsmount *mnt;
  2153. int err;
  2154. if (!fstype)
  2155. return -EINVAL;
  2156. type = get_fs_type(fstype);
  2157. if (!type)
  2158. return -ENODEV;
  2159. mnt = vfs_kern_mount(type, flags, name, data);
  2160. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  2161. !mnt->mnt_sb->s_subtype)
  2162. mnt = fs_set_subtype(mnt, fstype);
  2163. put_filesystem(type);
  2164. if (IS_ERR(mnt))
  2165. return PTR_ERR(mnt);
  2166. if (mount_too_revealing(mnt, &mnt_flags)) {
  2167. mntput(mnt);
  2168. return -EPERM;
  2169. }
  2170. err = do_add_mount(real_mount(mnt), path, mnt_flags);
  2171. if (err)
  2172. mntput(mnt);
  2173. return err;
  2174. }
  2175. int finish_automount(struct vfsmount *m, struct path *path)
  2176. {
  2177. struct mount *mnt = real_mount(m);
  2178. int err;
  2179. /* The new mount record should have at least 2 refs to prevent it being
  2180. * expired before we get a chance to add it
  2181. */
  2182. BUG_ON(mnt_get_count(mnt) < 2);
  2183. if (m->mnt_sb == path->mnt->mnt_sb &&
  2184. m->mnt_root == path->dentry) {
  2185. err = -ELOOP;
  2186. goto fail;
  2187. }
  2188. err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
  2189. if (!err)
  2190. return 0;
  2191. fail:
  2192. /* remove m from any expiration list it may be on */
  2193. if (!list_empty(&mnt->mnt_expire)) {
  2194. namespace_lock();
  2195. list_del_init(&mnt->mnt_expire);
  2196. namespace_unlock();
  2197. }
  2198. mntput(m);
  2199. mntput(m);
  2200. return err;
  2201. }
  2202. /**
  2203. * mnt_set_expiry - Put a mount on an expiration list
  2204. * @mnt: The mount to list.
  2205. * @expiry_list: The list to add the mount to.
  2206. */
  2207. void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
  2208. {
  2209. namespace_lock();
  2210. list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
  2211. namespace_unlock();
  2212. }
  2213. EXPORT_SYMBOL(mnt_set_expiry);
  2214. /*
  2215. * process a list of expirable mountpoints with the intent of discarding any
  2216. * mountpoints that aren't in use and haven't been touched since last we came
  2217. * here
  2218. */
  2219. void mark_mounts_for_expiry(struct list_head *mounts)
  2220. {
  2221. struct mount *mnt, *next;
  2222. LIST_HEAD(graveyard);
  2223. if (list_empty(mounts))
  2224. return;
  2225. namespace_lock();
  2226. lock_mount_hash();
  2227. /* extract from the expiration list every vfsmount that matches the
  2228. * following criteria:
  2229. * - only referenced by its parent vfsmount
  2230. * - still marked for expiry (marked on the last call here; marks are
  2231. * cleared by mntput())
  2232. */
  2233. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  2234. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  2235. propagate_mount_busy(mnt, 1))
  2236. continue;
  2237. list_move(&mnt->mnt_expire, &graveyard);
  2238. }
  2239. while (!list_empty(&graveyard)) {
  2240. mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
  2241. touch_mnt_namespace(mnt->mnt_ns);
  2242. umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
  2243. }
  2244. unlock_mount_hash();
  2245. namespace_unlock();
  2246. }
  2247. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  2248. /*
  2249. * Ripoff of 'select_parent()'
  2250. *
  2251. * search the list of submounts for a given mountpoint, and move any
  2252. * shrinkable submounts to the 'graveyard' list.
  2253. */
  2254. static int select_submounts(struct mount *parent, struct list_head *graveyard)
  2255. {
  2256. struct mount *this_parent = parent;
  2257. struct list_head *next;
  2258. int found = 0;
  2259. repeat:
  2260. next = this_parent->mnt_mounts.next;
  2261. resume:
  2262. while (next != &this_parent->mnt_mounts) {
  2263. struct list_head *tmp = next;
  2264. struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
  2265. next = tmp->next;
  2266. if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
  2267. continue;
  2268. /*
  2269. * Descend a level if the d_mounts list is non-empty.
  2270. */
  2271. if (!list_empty(&mnt->mnt_mounts)) {
  2272. this_parent = mnt;
  2273. goto repeat;
  2274. }
  2275. if (!propagate_mount_busy(mnt, 1)) {
  2276. list_move_tail(&mnt->mnt_expire, graveyard);
  2277. found++;
  2278. }
  2279. }
  2280. /*
  2281. * All done at this level ... ascend and resume the search
  2282. */
  2283. if (this_parent != parent) {
  2284. next = this_parent->mnt_child.next;
  2285. this_parent = this_parent->mnt_parent;
  2286. goto resume;
  2287. }
  2288. return found;
  2289. }
  2290. /*
  2291. * process a list of expirable mountpoints with the intent of discarding any
  2292. * submounts of a specific parent mountpoint
  2293. *
  2294. * mount_lock must be held for write
  2295. */
  2296. static void shrink_submounts(struct mount *mnt)
  2297. {
  2298. LIST_HEAD(graveyard);
  2299. struct mount *m;
  2300. /* extract submounts of 'mountpoint' from the expiration list */
  2301. while (select_submounts(mnt, &graveyard)) {
  2302. while (!list_empty(&graveyard)) {
  2303. m = list_first_entry(&graveyard, struct mount,
  2304. mnt_expire);
  2305. touch_mnt_namespace(m->mnt_ns);
  2306. umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
  2307. }
  2308. }
  2309. }
  2310. /*
  2311. * Some copy_from_user() implementations do not return the exact number of
  2312. * bytes remaining to copy on a fault. But copy_mount_options() requires that.
  2313. * Note that this function differs from copy_from_user() in that it will oops
  2314. * on bad values of `to', rather than returning a short copy.
  2315. */
  2316. static long exact_copy_from_user(void *to, const void __user * from,
  2317. unsigned long n)
  2318. {
  2319. char *t = to;
  2320. const char __user *f = from;
  2321. char c;
  2322. if (!access_ok(VERIFY_READ, from, n))
  2323. return n;
  2324. while (n) {
  2325. if (__get_user(c, f)) {
  2326. memset(t, 0, n);
  2327. break;
  2328. }
  2329. *t++ = c;
  2330. f++;
  2331. n--;
  2332. }
  2333. return n;
  2334. }
  2335. void *copy_mount_options(const void __user * data)
  2336. {
  2337. int i;
  2338. unsigned long size;
  2339. char *copy;
  2340. if (!data)
  2341. return NULL;
  2342. copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2343. if (!copy)
  2344. return ERR_PTR(-ENOMEM);
  2345. /* We only care that *some* data at the address the user
  2346. * gave us is valid. Just in case, we'll zero
  2347. * the remainder of the page.
  2348. */
  2349. /* copy_from_user cannot cross TASK_SIZE ! */
  2350. size = TASK_SIZE - (unsigned long)data;
  2351. if (size > PAGE_SIZE)
  2352. size = PAGE_SIZE;
  2353. i = size - exact_copy_from_user(copy, data, size);
  2354. if (!i) {
  2355. kfree(copy);
  2356. return ERR_PTR(-EFAULT);
  2357. }
  2358. if (i != PAGE_SIZE)
  2359. memset(copy + i, 0, PAGE_SIZE - i);
  2360. return copy;
  2361. }
  2362. char *copy_mount_string(const void __user *data)
  2363. {
  2364. return data ? strndup_user(data, PAGE_SIZE) : NULL;
  2365. }
  2366. /*
  2367. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  2368. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  2369. *
  2370. * data is a (void *) that can point to any structure up to
  2371. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  2372. * information (or be NULL).
  2373. *
  2374. * Pre-0.97 versions of mount() didn't have a flags word.
  2375. * When the flags word was introduced its top half was required
  2376. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  2377. * Therefore, if this magic number is present, it carries no information
  2378. * and must be discarded.
  2379. */
  2380. long do_mount(const char *dev_name, const char __user *dir_name,
  2381. const char *type_page, unsigned long flags, void *data_page)
  2382. {
  2383. struct path path;
  2384. int retval = 0;
  2385. int mnt_flags = 0;
  2386. /* Discard magic */
  2387. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  2388. flags &= ~MS_MGC_MSK;
  2389. /* Basic sanity checks */
  2390. if (data_page)
  2391. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  2392. /* ... and get the mountpoint */
  2393. retval = user_path(dir_name, &path);
  2394. if (retval)
  2395. return retval;
  2396. retval = security_sb_mount(dev_name, &path,
  2397. type_page, flags, data_page);
  2398. if (!retval && !may_mount())
  2399. retval = -EPERM;
  2400. if (!retval && (flags & MS_MANDLOCK) && !may_mandlock())
  2401. retval = -EPERM;
  2402. if (retval)
  2403. goto dput_out;
  2404. /* Default to relatime unless overriden */
  2405. if (!(flags & MS_NOATIME))
  2406. mnt_flags |= MNT_RELATIME;
  2407. /* Separate the per-mountpoint flags */
  2408. if (flags & MS_NOSUID)
  2409. mnt_flags |= MNT_NOSUID;
  2410. if (flags & MS_NODEV)
  2411. mnt_flags |= MNT_NODEV;
  2412. if (flags & MS_NOEXEC)
  2413. mnt_flags |= MNT_NOEXEC;
  2414. if (flags & MS_NOATIME)
  2415. mnt_flags |= MNT_NOATIME;
  2416. if (flags & MS_NODIRATIME)
  2417. mnt_flags |= MNT_NODIRATIME;
  2418. if (flags & MS_STRICTATIME)
  2419. mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
  2420. if (flags & MS_RDONLY)
  2421. mnt_flags |= MNT_READONLY;
  2422. /* The default atime for remount is preservation */
  2423. if ((flags & MS_REMOUNT) &&
  2424. ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
  2425. MS_STRICTATIME)) == 0)) {
  2426. mnt_flags &= ~MNT_ATIME_MASK;
  2427. mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
  2428. }
  2429. flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
  2430. MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
  2431. MS_STRICTATIME | MS_NOREMOTELOCK);
  2432. if (flags & MS_REMOUNT)
  2433. retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
  2434. data_page);
  2435. else if (flags & MS_BIND)
  2436. retval = do_loopback(&path, dev_name, flags & MS_REC);
  2437. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  2438. retval = do_change_type(&path, flags);
  2439. else if (flags & MS_MOVE)
  2440. retval = do_move_mount(&path, dev_name);
  2441. else
  2442. retval = do_new_mount(&path, type_page, flags, mnt_flags,
  2443. dev_name, data_page);
  2444. dput_out:
  2445. path_put(&path);
  2446. return retval;
  2447. }
  2448. static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
  2449. {
  2450. return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
  2451. }
  2452. static void dec_mnt_namespaces(struct ucounts *ucounts)
  2453. {
  2454. dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
  2455. }
  2456. static void free_mnt_ns(struct mnt_namespace *ns)
  2457. {
  2458. ns_free_inum(&ns->ns);
  2459. dec_mnt_namespaces(ns->ucounts);
  2460. put_user_ns(ns->user_ns);
  2461. kfree(ns);
  2462. }
  2463. /*
  2464. * Assign a sequence number so we can detect when we attempt to bind
  2465. * mount a reference to an older mount namespace into the current
  2466. * mount namespace, preventing reference counting loops. A 64bit
  2467. * number incrementing at 10Ghz will take 12,427 years to wrap which
  2468. * is effectively never, so we can ignore the possibility.
  2469. */
  2470. static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
  2471. static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
  2472. {
  2473. struct mnt_namespace *new_ns;
  2474. struct ucounts *ucounts;
  2475. int ret;
  2476. ucounts = inc_mnt_namespaces(user_ns);
  2477. if (!ucounts)
  2478. return ERR_PTR(-ENOSPC);
  2479. new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
  2480. if (!new_ns) {
  2481. dec_mnt_namespaces(ucounts);
  2482. return ERR_PTR(-ENOMEM);
  2483. }
  2484. ret = ns_alloc_inum(&new_ns->ns);
  2485. if (ret) {
  2486. kfree(new_ns);
  2487. dec_mnt_namespaces(ucounts);
  2488. return ERR_PTR(ret);
  2489. }
  2490. new_ns->ns.ops = &mntns_operations;
  2491. new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
  2492. atomic_set(&new_ns->count, 1);
  2493. new_ns->root = NULL;
  2494. INIT_LIST_HEAD(&new_ns->list);
  2495. init_waitqueue_head(&new_ns->poll);
  2496. new_ns->event = 0;
  2497. new_ns->user_ns = get_user_ns(user_ns);
  2498. new_ns->ucounts = ucounts;
  2499. new_ns->mounts = 0;
  2500. new_ns->pending_mounts = 0;
  2501. return new_ns;
  2502. }
  2503. __latent_entropy
  2504. struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
  2505. struct user_namespace *user_ns, struct fs_struct *new_fs)
  2506. {
  2507. struct mnt_namespace *new_ns;
  2508. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
  2509. struct mount *p, *q;
  2510. struct mount *old;
  2511. struct mount *new;
  2512. int copy_flags;
  2513. BUG_ON(!ns);
  2514. if (likely(!(flags & CLONE_NEWNS))) {
  2515. get_mnt_ns(ns);
  2516. return ns;
  2517. }
  2518. old = ns->root;
  2519. new_ns = alloc_mnt_ns(user_ns);
  2520. if (IS_ERR(new_ns))
  2521. return new_ns;
  2522. namespace_lock();
  2523. /* First pass: copy the tree topology */
  2524. copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
  2525. if (user_ns != ns->user_ns)
  2526. copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
  2527. new = copy_tree(old, old->mnt.mnt_root, copy_flags);
  2528. if (IS_ERR(new)) {
  2529. namespace_unlock();
  2530. free_mnt_ns(new_ns);
  2531. return ERR_CAST(new);
  2532. }
  2533. new_ns->root = new;
  2534. list_add_tail(&new_ns->list, &new->mnt_list);
  2535. /*
  2536. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  2537. * as belonging to new namespace. We have already acquired a private
  2538. * fs_struct, so tsk->fs->lock is not needed.
  2539. */
  2540. p = old;
  2541. q = new;
  2542. while (p) {
  2543. q->mnt_ns = new_ns;
  2544. new_ns->mounts++;
  2545. if (new_fs) {
  2546. if (&p->mnt == new_fs->root.mnt) {
  2547. new_fs->root.mnt = mntget(&q->mnt);
  2548. rootmnt = &p->mnt;
  2549. }
  2550. if (&p->mnt == new_fs->pwd.mnt) {
  2551. new_fs->pwd.mnt = mntget(&q->mnt);
  2552. pwdmnt = &p->mnt;
  2553. }
  2554. }
  2555. p = next_mnt(p, old);
  2556. q = next_mnt(q, new);
  2557. if (!q)
  2558. break;
  2559. while (p->mnt.mnt_root != q->mnt.mnt_root)
  2560. p = next_mnt(p, old);
  2561. }
  2562. namespace_unlock();
  2563. if (rootmnt)
  2564. mntput(rootmnt);
  2565. if (pwdmnt)
  2566. mntput(pwdmnt);
  2567. return new_ns;
  2568. }
  2569. /**
  2570. * create_mnt_ns - creates a private namespace and adds a root filesystem
  2571. * @mnt: pointer to the new root filesystem mountpoint
  2572. */
  2573. static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
  2574. {
  2575. struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
  2576. if (!IS_ERR(new_ns)) {
  2577. struct mount *mnt = real_mount(m);
  2578. mnt->mnt_ns = new_ns;
  2579. new_ns->root = mnt;
  2580. new_ns->mounts++;
  2581. list_add(&mnt->mnt_list, &new_ns->list);
  2582. } else {
  2583. mntput(m);
  2584. }
  2585. return new_ns;
  2586. }
  2587. struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
  2588. {
  2589. struct mnt_namespace *ns;
  2590. struct super_block *s;
  2591. struct path path;
  2592. int err;
  2593. ns = create_mnt_ns(mnt);
  2594. if (IS_ERR(ns))
  2595. return ERR_CAST(ns);
  2596. err = vfs_path_lookup(mnt->mnt_root, mnt,
  2597. name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  2598. put_mnt_ns(ns);
  2599. if (err)
  2600. return ERR_PTR(err);
  2601. /* trade a vfsmount reference for active sb one */
  2602. s = path.mnt->mnt_sb;
  2603. atomic_inc(&s->s_active);
  2604. mntput(path.mnt);
  2605. /* lock the sucker */
  2606. down_write(&s->s_umount);
  2607. /* ... and return the root of (sub)tree on it */
  2608. return path.dentry;
  2609. }
  2610. EXPORT_SYMBOL(mount_subtree);
  2611. SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
  2612. char __user *, type, unsigned long, flags, void __user *, data)
  2613. {
  2614. int ret;
  2615. char *kernel_type;
  2616. char *kernel_dev;
  2617. void *options;
  2618. kernel_type = copy_mount_string(type);
  2619. ret = PTR_ERR(kernel_type);
  2620. if (IS_ERR(kernel_type))
  2621. goto out_type;
  2622. kernel_dev = copy_mount_string(dev_name);
  2623. ret = PTR_ERR(kernel_dev);
  2624. if (IS_ERR(kernel_dev))
  2625. goto out_dev;
  2626. options = copy_mount_options(data);
  2627. ret = PTR_ERR(options);
  2628. if (IS_ERR(options))
  2629. goto out_data;
  2630. ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
  2631. kfree(options);
  2632. out_data:
  2633. kfree(kernel_dev);
  2634. out_dev:
  2635. kfree(kernel_type);
  2636. out_type:
  2637. return ret;
  2638. }
  2639. /*
  2640. * Return true if path is reachable from root
  2641. *
  2642. * namespace_sem or mount_lock is held
  2643. */
  2644. bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
  2645. const struct path *root)
  2646. {
  2647. while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
  2648. dentry = mnt->mnt_mountpoint;
  2649. mnt = mnt->mnt_parent;
  2650. }
  2651. return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
  2652. }
  2653. bool path_is_under(const struct path *path1, const struct path *path2)
  2654. {
  2655. bool res;
  2656. read_seqlock_excl(&mount_lock);
  2657. res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
  2658. read_sequnlock_excl(&mount_lock);
  2659. return res;
  2660. }
  2661. EXPORT_SYMBOL(path_is_under);
  2662. /*
  2663. * pivot_root Semantics:
  2664. * Moves the root file system of the current process to the directory put_old,
  2665. * makes new_root as the new root file system of the current process, and sets
  2666. * root/cwd of all processes which had them on the current root to new_root.
  2667. *
  2668. * Restrictions:
  2669. * The new_root and put_old must be directories, and must not be on the
  2670. * same file system as the current process root. The put_old must be
  2671. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  2672. * pointed to by put_old must yield the same directory as new_root. No other
  2673. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  2674. *
  2675. * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
  2676. * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
  2677. * in this situation.
  2678. *
  2679. * Notes:
  2680. * - we don't move root/cwd if they are not at the root (reason: if something
  2681. * cared enough to change them, it's probably wrong to force them elsewhere)
  2682. * - it's okay to pick a root that isn't the root of a file system, e.g.
  2683. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  2684. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  2685. * first.
  2686. */
  2687. SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
  2688. const char __user *, put_old)
  2689. {
  2690. struct path new, old, parent_path, root_parent, root;
  2691. struct mount *new_mnt, *root_mnt, *old_mnt;
  2692. struct mountpoint *old_mp, *root_mp;
  2693. int error;
  2694. if (!may_mount())
  2695. return -EPERM;
  2696. error = user_path_dir(new_root, &new);
  2697. if (error)
  2698. goto out0;
  2699. error = user_path_dir(put_old, &old);
  2700. if (error)
  2701. goto out1;
  2702. error = security_sb_pivotroot(&old, &new);
  2703. if (error)
  2704. goto out2;
  2705. get_fs_root(current->fs, &root);
  2706. old_mp = lock_mount(&old);
  2707. error = PTR_ERR(old_mp);
  2708. if (IS_ERR(old_mp))
  2709. goto out3;
  2710. error = -EINVAL;
  2711. new_mnt = real_mount(new.mnt);
  2712. root_mnt = real_mount(root.mnt);
  2713. old_mnt = real_mount(old.mnt);
  2714. if (IS_MNT_SHARED(old_mnt) ||
  2715. IS_MNT_SHARED(new_mnt->mnt_parent) ||
  2716. IS_MNT_SHARED(root_mnt->mnt_parent))
  2717. goto out4;
  2718. if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
  2719. goto out4;
  2720. if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
  2721. goto out4;
  2722. error = -ENOENT;
  2723. if (d_unlinked(new.dentry))
  2724. goto out4;
  2725. error = -EBUSY;
  2726. if (new_mnt == root_mnt || old_mnt == root_mnt)
  2727. goto out4; /* loop, on the same file system */
  2728. error = -EINVAL;
  2729. if (root.mnt->mnt_root != root.dentry)
  2730. goto out4; /* not a mountpoint */
  2731. if (!mnt_has_parent(root_mnt))
  2732. goto out4; /* not attached */
  2733. root_mp = root_mnt->mnt_mp;
  2734. if (new.mnt->mnt_root != new.dentry)
  2735. goto out4; /* not a mountpoint */
  2736. if (!mnt_has_parent(new_mnt))
  2737. goto out4; /* not attached */
  2738. /* make sure we can reach put_old from new_root */
  2739. if (!is_path_reachable(old_mnt, old.dentry, &new))
  2740. goto out4;
  2741. /* make certain new is below the root */
  2742. if (!is_path_reachable(new_mnt, new.dentry, &root))
  2743. goto out4;
  2744. root_mp->m_count++; /* pin it so it won't go away */
  2745. lock_mount_hash();
  2746. detach_mnt(new_mnt, &parent_path);
  2747. detach_mnt(root_mnt, &root_parent);
  2748. if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
  2749. new_mnt->mnt.mnt_flags |= MNT_LOCKED;
  2750. root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
  2751. }
  2752. /* mount old root on put_old */
  2753. attach_mnt(root_mnt, old_mnt, old_mp);
  2754. /* mount new_root on / */
  2755. attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
  2756. touch_mnt_namespace(current->nsproxy->mnt_ns);
  2757. /* A moved mount should not expire automatically */
  2758. list_del_init(&new_mnt->mnt_expire);
  2759. unlock_mount_hash();
  2760. chroot_fs_refs(&root, &new);
  2761. put_mountpoint(root_mp);
  2762. error = 0;
  2763. out4:
  2764. unlock_mount(old_mp);
  2765. if (!error) {
  2766. path_put(&root_parent);
  2767. path_put(&parent_path);
  2768. }
  2769. out3:
  2770. path_put(&root);
  2771. out2:
  2772. path_put(&old);
  2773. out1:
  2774. path_put(&new);
  2775. out0:
  2776. return error;
  2777. }
  2778. static void __init init_mount_tree(void)
  2779. {
  2780. struct vfsmount *mnt;
  2781. struct mnt_namespace *ns;
  2782. struct path root;
  2783. struct file_system_type *type;
  2784. type = get_fs_type("rootfs");
  2785. if (!type)
  2786. panic("Can't find rootfs type");
  2787. mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
  2788. put_filesystem(type);
  2789. if (IS_ERR(mnt))
  2790. panic("Can't create rootfs");
  2791. ns = create_mnt_ns(mnt);
  2792. if (IS_ERR(ns))
  2793. panic("Can't allocate initial namespace");
  2794. init_task.nsproxy->mnt_ns = ns;
  2795. get_mnt_ns(ns);
  2796. root.mnt = mnt;
  2797. root.dentry = mnt->mnt_root;
  2798. mnt->mnt_flags |= MNT_LOCKED;
  2799. set_fs_pwd(current->fs, &root);
  2800. set_fs_root(current->fs, &root);
  2801. }
  2802. void __init mnt_init(void)
  2803. {
  2804. unsigned u;
  2805. int err;
  2806. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
  2807. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2808. mount_hashtable = alloc_large_system_hash("Mount-cache",
  2809. sizeof(struct hlist_head),
  2810. mhash_entries, 19,
  2811. 0,
  2812. &m_hash_shift, &m_hash_mask, 0, 0);
  2813. mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
  2814. sizeof(struct hlist_head),
  2815. mphash_entries, 19,
  2816. 0,
  2817. &mp_hash_shift, &mp_hash_mask, 0, 0);
  2818. if (!mount_hashtable || !mountpoint_hashtable)
  2819. panic("Failed to allocate mount hash table\n");
  2820. for (u = 0; u <= m_hash_mask; u++)
  2821. INIT_HLIST_HEAD(&mount_hashtable[u]);
  2822. for (u = 0; u <= mp_hash_mask; u++)
  2823. INIT_HLIST_HEAD(&mountpoint_hashtable[u]);
  2824. kernfs_init();
  2825. err = sysfs_init();
  2826. if (err)
  2827. printk(KERN_WARNING "%s: sysfs_init error: %d\n",
  2828. __func__, err);
  2829. fs_kobj = kobject_create_and_add("fs", NULL);
  2830. if (!fs_kobj)
  2831. printk(KERN_WARNING "%s: kobj create error\n", __func__);
  2832. init_rootfs();
  2833. init_mount_tree();
  2834. }
  2835. void put_mnt_ns(struct mnt_namespace *ns)
  2836. {
  2837. if (!atomic_dec_and_test(&ns->count))
  2838. return;
  2839. drop_collected_mounts(&ns->root->mnt);
  2840. free_mnt_ns(ns);
  2841. }
  2842. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  2843. {
  2844. struct vfsmount *mnt;
  2845. mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  2846. if (!IS_ERR(mnt)) {
  2847. /*
  2848. * it is a longterm mount, don't release mnt until
  2849. * we unmount before file sys is unregistered
  2850. */
  2851. real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
  2852. }
  2853. return mnt;
  2854. }
  2855. EXPORT_SYMBOL_GPL(kern_mount_data);
  2856. void kern_unmount(struct vfsmount *mnt)
  2857. {
  2858. /* release long term mount so mount point can be released */
  2859. if (!IS_ERR_OR_NULL(mnt)) {
  2860. real_mount(mnt)->mnt_ns = NULL;
  2861. synchronize_rcu(); /* yecchhh... */
  2862. mntput(mnt);
  2863. }
  2864. }
  2865. EXPORT_SYMBOL(kern_unmount);
  2866. bool our_mnt(struct vfsmount *mnt)
  2867. {
  2868. return check_mnt(real_mount(mnt));
  2869. }
  2870. bool current_chrooted(void)
  2871. {
  2872. /* Does the current process have a non-standard root */
  2873. struct path ns_root;
  2874. struct path fs_root;
  2875. bool chrooted;
  2876. /* Find the namespace root */
  2877. ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
  2878. ns_root.dentry = ns_root.mnt->mnt_root;
  2879. path_get(&ns_root);
  2880. while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
  2881. ;
  2882. get_fs_root(current->fs, &fs_root);
  2883. chrooted = !path_equal(&fs_root, &ns_root);
  2884. path_put(&fs_root);
  2885. path_put(&ns_root);
  2886. return chrooted;
  2887. }
  2888. static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new,
  2889. int *new_mnt_flags)
  2890. {
  2891. int new_flags = *new_mnt_flags;
  2892. struct mount *mnt;
  2893. bool visible = false;
  2894. down_read(&namespace_sem);
  2895. list_for_each_entry(mnt, &ns->list, mnt_list) {
  2896. struct mount *child;
  2897. int mnt_flags;
  2898. if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type)
  2899. continue;
  2900. /* This mount is not fully visible if it's root directory
  2901. * is not the root directory of the filesystem.
  2902. */
  2903. if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
  2904. continue;
  2905. /* A local view of the mount flags */
  2906. mnt_flags = mnt->mnt.mnt_flags;
  2907. /* Don't miss readonly hidden in the superblock flags */
  2908. if (mnt->mnt.mnt_sb->s_flags & MS_RDONLY)
  2909. mnt_flags |= MNT_LOCK_READONLY;
  2910. /* Verify the mount flags are equal to or more permissive
  2911. * than the proposed new mount.
  2912. */
  2913. if ((mnt_flags & MNT_LOCK_READONLY) &&
  2914. !(new_flags & MNT_READONLY))
  2915. continue;
  2916. if ((mnt_flags & MNT_LOCK_ATIME) &&
  2917. ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
  2918. continue;
  2919. /* This mount is not fully visible if there are any
  2920. * locked child mounts that cover anything except for
  2921. * empty directories.
  2922. */
  2923. list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
  2924. struct inode *inode = child->mnt_mountpoint->d_inode;
  2925. /* Only worry about locked mounts */
  2926. if (!(child->mnt.mnt_flags & MNT_LOCKED))
  2927. continue;
  2928. /* Is the directory permanetly empty? */
  2929. if (!is_empty_dir_inode(inode))
  2930. goto next;
  2931. }
  2932. /* Preserve the locked attributes */
  2933. *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
  2934. MNT_LOCK_ATIME);
  2935. visible = true;
  2936. goto found;
  2937. next: ;
  2938. }
  2939. found:
  2940. up_read(&namespace_sem);
  2941. return visible;
  2942. }
  2943. static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags)
  2944. {
  2945. const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
  2946. struct mnt_namespace *ns = current->nsproxy->mnt_ns;
  2947. unsigned long s_iflags;
  2948. if (ns->user_ns == &init_user_ns)
  2949. return false;
  2950. /* Can this filesystem be too revealing? */
  2951. s_iflags = mnt->mnt_sb->s_iflags;
  2952. if (!(s_iflags & SB_I_USERNS_VISIBLE))
  2953. return false;
  2954. if ((s_iflags & required_iflags) != required_iflags) {
  2955. WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
  2956. required_iflags);
  2957. return true;
  2958. }
  2959. return !mnt_already_visible(ns, mnt, new_mnt_flags);
  2960. }
  2961. bool mnt_may_suid(struct vfsmount *mnt)
  2962. {
  2963. /*
  2964. * Foreign mounts (accessed via fchdir or through /proc
  2965. * symlinks) are always treated as if they are nosuid. This
  2966. * prevents namespaces from trusting potentially unsafe
  2967. * suid/sgid bits, file caps, or security labels that originate
  2968. * in other namespaces.
  2969. */
  2970. return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
  2971. current_in_userns(mnt->mnt_sb->s_user_ns);
  2972. }
  2973. static struct ns_common *mntns_get(struct task_struct *task)
  2974. {
  2975. struct ns_common *ns = NULL;
  2976. struct nsproxy *nsproxy;
  2977. task_lock(task);
  2978. nsproxy = task->nsproxy;
  2979. if (nsproxy) {
  2980. ns = &nsproxy->mnt_ns->ns;
  2981. get_mnt_ns(to_mnt_ns(ns));
  2982. }
  2983. task_unlock(task);
  2984. return ns;
  2985. }
  2986. static void mntns_put(struct ns_common *ns)
  2987. {
  2988. put_mnt_ns(to_mnt_ns(ns));
  2989. }
  2990. static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  2991. {
  2992. struct fs_struct *fs = current->fs;
  2993. struct mnt_namespace *mnt_ns = to_mnt_ns(ns);
  2994. struct path root;
  2995. if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
  2996. !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
  2997. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  2998. return -EPERM;
  2999. if (fs->users != 1)
  3000. return -EINVAL;
  3001. get_mnt_ns(mnt_ns);
  3002. put_mnt_ns(nsproxy->mnt_ns);
  3003. nsproxy->mnt_ns = mnt_ns;
  3004. /* Find the root */
  3005. root.mnt = &mnt_ns->root->mnt;
  3006. root.dentry = mnt_ns->root->mnt.mnt_root;
  3007. path_get(&root);
  3008. while(d_mountpoint(root.dentry) && follow_down_one(&root))
  3009. ;
  3010. /* Update the pwd and root */
  3011. set_fs_pwd(fs, &root);
  3012. set_fs_root(fs, &root);
  3013. path_put(&root);
  3014. return 0;
  3015. }
  3016. static struct user_namespace *mntns_owner(struct ns_common *ns)
  3017. {
  3018. return to_mnt_ns(ns)->user_ns;
  3019. }
  3020. const struct proc_ns_operations mntns_operations = {
  3021. .name = "mnt",
  3022. .type = CLONE_NEWNS,
  3023. .get = mntns_get,
  3024. .put = mntns_put,
  3025. .install = mntns_install,
  3026. .owner = mntns_owner,
  3027. };