shmem.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968
  1. /*
  2. * Resizable virtual memory filesystem for Linux.
  3. *
  4. * Copyright (C) 2000 Linus Torvalds.
  5. * 2000 Transmeta Corp.
  6. * 2000-2001 Christoph Rohland
  7. * 2000-2001 SAP AG
  8. * 2002 Red Hat Inc.
  9. * Copyright (C) 2002-2011 Hugh Dickins.
  10. * Copyright (C) 2011 Google Inc.
  11. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  12. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  13. *
  14. * Extended attribute support for tmpfs:
  15. * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  16. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  17. *
  18. * tiny-shmem:
  19. * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
  20. *
  21. * This file is released under the GPL.
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/init.h>
  25. #include <linux/vfs.h>
  26. #include <linux/mount.h>
  27. #include <linux/ramfs.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/file.h>
  30. #include <linux/mm.h>
  31. #include <linux/export.h>
  32. #include <linux/swap.h>
  33. #include <linux/aio.h>
  34. static struct vfsmount *shm_mnt;
  35. #ifdef CONFIG_SHMEM
  36. /*
  37. * This virtual memory filesystem is heavily based on the ramfs. It
  38. * extends ramfs by the ability to use swap and honor resource limits
  39. * which makes it a completely usable filesystem.
  40. */
  41. #include <linux/xattr.h>
  42. #include <linux/exportfs.h>
  43. #include <linux/posix_acl.h>
  44. #include <linux/posix_acl_xattr.h>
  45. #include <linux/mman.h>
  46. #include <linux/string.h>
  47. #include <linux/slab.h>
  48. #include <linux/backing-dev.h>
  49. #include <linux/shmem_fs.h>
  50. #include <linux/writeback.h>
  51. #include <linux/blkdev.h>
  52. #include <linux/pagevec.h>
  53. #include <linux/percpu_counter.h>
  54. #include <linux/falloc.h>
  55. #include <linux/splice.h>
  56. #include <linux/security.h>
  57. #include <linux/swapops.h>
  58. #include <linux/mempolicy.h>
  59. #include <linux/namei.h>
  60. #include <linux/ctype.h>
  61. #include <linux/migrate.h>
  62. #include <linux/highmem.h>
  63. #include <linux/seq_file.h>
  64. #include <linux/magic.h>
  65. #include <asm/uaccess.h>
  66. #include <asm/pgtable.h>
  67. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  68. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  69. /* Pretend that each entry is of this size in directory's i_size */
  70. #define BOGO_DIRENT_SIZE 20
  71. /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
  72. #define SHORT_SYMLINK_LEN 128
  73. /*
  74. * shmem_fallocate and shmem_writepage communicate via inode->i_private
  75. * (with i_mutex making sure that it has only one user at a time):
  76. * we would prefer not to enlarge the shmem inode just for that.
  77. */
  78. struct shmem_falloc {
  79. pgoff_t start; /* start of range currently being fallocated */
  80. pgoff_t next; /* the next page offset to be fallocated */
  81. pgoff_t nr_falloced; /* how many new pages have been fallocated */
  82. pgoff_t nr_unswapped; /* how often writepage refused to swap out */
  83. };
  84. /* Flag allocation requirements to shmem_getpage */
  85. enum sgp_type {
  86. SGP_READ, /* don't exceed i_size, don't allocate page */
  87. SGP_CACHE, /* don't exceed i_size, may allocate page */
  88. SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
  89. SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
  90. SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
  91. };
  92. #ifdef CONFIG_TMPFS
  93. static unsigned long shmem_default_max_blocks(void)
  94. {
  95. return totalram_pages / 2;
  96. }
  97. static unsigned long shmem_default_max_inodes(void)
  98. {
  99. return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
  100. }
  101. #endif
  102. static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
  103. static int shmem_replace_page(struct page **pagep, gfp_t gfp,
  104. struct shmem_inode_info *info, pgoff_t index);
  105. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  106. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
  107. static inline int shmem_getpage(struct inode *inode, pgoff_t index,
  108. struct page **pagep, enum sgp_type sgp, int *fault_type)
  109. {
  110. return shmem_getpage_gfp(inode, index, pagep, sgp,
  111. mapping_gfp_mask(inode->i_mapping), fault_type);
  112. }
  113. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  114. {
  115. return sb->s_fs_info;
  116. }
  117. /*
  118. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  119. * for shared memory and for shared anonymous (/dev/zero) mappings
  120. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  121. * consistent with the pre-accounting of private mappings ...
  122. */
  123. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  124. {
  125. return (flags & VM_NORESERVE) ?
  126. 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
  127. }
  128. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  129. {
  130. if (!(flags & VM_NORESERVE))
  131. vm_unacct_memory(VM_ACCT(size));
  132. }
  133. /*
  134. * ... whereas tmpfs objects are accounted incrementally as
  135. * pages are allocated, in order to allow huge sparse files.
  136. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  137. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  138. */
  139. static inline int shmem_acct_block(unsigned long flags)
  140. {
  141. return (flags & VM_NORESERVE) ?
  142. security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
  143. }
  144. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  145. {
  146. if (flags & VM_NORESERVE)
  147. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  148. }
  149. static const struct super_operations shmem_ops;
  150. static const struct address_space_operations shmem_aops;
  151. static const struct file_operations shmem_file_operations;
  152. static const struct inode_operations shmem_inode_operations;
  153. static const struct inode_operations shmem_dir_inode_operations;
  154. static const struct inode_operations shmem_special_inode_operations;
  155. static const struct vm_operations_struct shmem_vm_ops;
  156. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  157. .ra_pages = 0, /* No readahead */
  158. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
  159. };
  160. static LIST_HEAD(shmem_swaplist);
  161. static DEFINE_MUTEX(shmem_swaplist_mutex);
  162. static int shmem_reserve_inode(struct super_block *sb)
  163. {
  164. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  165. if (sbinfo->max_inodes) {
  166. spin_lock(&sbinfo->stat_lock);
  167. if (!sbinfo->free_inodes) {
  168. spin_unlock(&sbinfo->stat_lock);
  169. return -ENOSPC;
  170. }
  171. sbinfo->free_inodes--;
  172. spin_unlock(&sbinfo->stat_lock);
  173. }
  174. return 0;
  175. }
  176. static void shmem_free_inode(struct super_block *sb)
  177. {
  178. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  179. if (sbinfo->max_inodes) {
  180. spin_lock(&sbinfo->stat_lock);
  181. sbinfo->free_inodes++;
  182. spin_unlock(&sbinfo->stat_lock);
  183. }
  184. }
  185. /**
  186. * shmem_recalc_inode - recalculate the block usage of an inode
  187. * @inode: inode to recalc
  188. *
  189. * We have to calculate the free blocks since the mm can drop
  190. * undirtied hole pages behind our back.
  191. *
  192. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  193. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  194. *
  195. * It has to be called with the spinlock held.
  196. */
  197. static void shmem_recalc_inode(struct inode *inode)
  198. {
  199. struct shmem_inode_info *info = SHMEM_I(inode);
  200. long freed;
  201. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  202. if (freed > 0) {
  203. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  204. if (sbinfo->max_blocks)
  205. percpu_counter_add(&sbinfo->used_blocks, -freed);
  206. info->alloced -= freed;
  207. inode->i_blocks -= freed * BLOCKS_PER_PAGE;
  208. shmem_unacct_blocks(info->flags, freed);
  209. }
  210. }
  211. /*
  212. * Replace item expected in radix tree by a new item, while holding tree lock.
  213. */
  214. static int shmem_radix_tree_replace(struct address_space *mapping,
  215. pgoff_t index, void *expected, void *replacement)
  216. {
  217. void **pslot;
  218. void *item;
  219. VM_BUG_ON(!expected);
  220. VM_BUG_ON(!replacement);
  221. pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
  222. if (!pslot)
  223. return -ENOENT;
  224. item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock);
  225. if (item != expected)
  226. return -ENOENT;
  227. radix_tree_replace_slot(pslot, replacement);
  228. return 0;
  229. }
  230. /*
  231. * Sometimes, before we decide whether to proceed or to fail, we must check
  232. * that an entry was not already brought back from swap by a racing thread.
  233. *
  234. * Checking page is not enough: by the time a SwapCache page is locked, it
  235. * might be reused, and again be SwapCache, using the same swap as before.
  236. */
  237. static bool shmem_confirm_swap(struct address_space *mapping,
  238. pgoff_t index, swp_entry_t swap)
  239. {
  240. void *item;
  241. rcu_read_lock();
  242. item = radix_tree_lookup(&mapping->page_tree, index);
  243. rcu_read_unlock();
  244. return item == swp_to_radix_entry(swap);
  245. }
  246. /*
  247. * Like add_to_page_cache_locked, but error if expected item has gone.
  248. */
  249. static int shmem_add_to_page_cache(struct page *page,
  250. struct address_space *mapping,
  251. pgoff_t index, gfp_t gfp, void *expected)
  252. {
  253. int error;
  254. VM_BUG_ON_PAGE(!PageLocked(page), page);
  255. VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
  256. page_cache_get(page);
  257. page->mapping = mapping;
  258. page->index = index;
  259. spin_lock_irq(&mapping->tree_lock);
  260. if (!expected)
  261. error = radix_tree_insert(&mapping->page_tree, index, page);
  262. else
  263. error = shmem_radix_tree_replace(mapping, index, expected,
  264. page);
  265. if (!error) {
  266. mapping->nrpages++;
  267. __inc_zone_page_state(page, NR_FILE_PAGES);
  268. __inc_zone_page_state(page, NR_SHMEM);
  269. spin_unlock_irq(&mapping->tree_lock);
  270. } else {
  271. page->mapping = NULL;
  272. spin_unlock_irq(&mapping->tree_lock);
  273. page_cache_release(page);
  274. }
  275. return error;
  276. }
  277. /*
  278. * Like delete_from_page_cache, but substitutes swap for page.
  279. */
  280. static void shmem_delete_from_page_cache(struct page *page, void *radswap)
  281. {
  282. struct address_space *mapping = page->mapping;
  283. int error;
  284. spin_lock_irq(&mapping->tree_lock);
  285. error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
  286. page->mapping = NULL;
  287. mapping->nrpages--;
  288. __dec_zone_page_state(page, NR_FILE_PAGES);
  289. __dec_zone_page_state(page, NR_SHMEM);
  290. spin_unlock_irq(&mapping->tree_lock);
  291. page_cache_release(page);
  292. BUG_ON(error);
  293. }
  294. /*
  295. * Remove swap entry from radix tree, free the swap and its page cache.
  296. */
  297. static int shmem_free_swap(struct address_space *mapping,
  298. pgoff_t index, void *radswap)
  299. {
  300. void *old;
  301. spin_lock_irq(&mapping->tree_lock);
  302. old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
  303. spin_unlock_irq(&mapping->tree_lock);
  304. if (old != radswap)
  305. return -ENOENT;
  306. free_swap_and_cache(radix_to_swp_entry(radswap));
  307. return 0;
  308. }
  309. /*
  310. * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
  311. */
  312. void shmem_unlock_mapping(struct address_space *mapping)
  313. {
  314. struct pagevec pvec;
  315. pgoff_t indices[PAGEVEC_SIZE];
  316. pgoff_t index = 0;
  317. pagevec_init(&pvec, 0);
  318. /*
  319. * Minor point, but we might as well stop if someone else SHM_LOCKs it.
  320. */
  321. while (!mapping_unevictable(mapping)) {
  322. /*
  323. * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
  324. * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
  325. */
  326. pvec.nr = find_get_entries(mapping, index,
  327. PAGEVEC_SIZE, pvec.pages, indices);
  328. if (!pvec.nr)
  329. break;
  330. index = indices[pvec.nr - 1] + 1;
  331. pagevec_remove_exceptionals(&pvec);
  332. check_move_unevictable_pages(pvec.pages, pvec.nr);
  333. pagevec_release(&pvec);
  334. cond_resched();
  335. }
  336. }
  337. /*
  338. * Remove range of pages and swap entries from radix tree, and free them.
  339. * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
  340. */
  341. static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
  342. bool unfalloc)
  343. {
  344. struct address_space *mapping = inode->i_mapping;
  345. struct shmem_inode_info *info = SHMEM_I(inode);
  346. pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  347. pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
  348. unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
  349. unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
  350. struct pagevec pvec;
  351. pgoff_t indices[PAGEVEC_SIZE];
  352. long nr_swaps_freed = 0;
  353. pgoff_t index;
  354. int i;
  355. if (lend == -1)
  356. end = -1; /* unsigned, so actually very big */
  357. pagevec_init(&pvec, 0);
  358. index = start;
  359. while (index < end) {
  360. pvec.nr = find_get_entries(mapping, index,
  361. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  362. pvec.pages, indices);
  363. if (!pvec.nr)
  364. break;
  365. mem_cgroup_uncharge_start();
  366. for (i = 0; i < pagevec_count(&pvec); i++) {
  367. struct page *page = pvec.pages[i];
  368. index = indices[i];
  369. if (index >= end)
  370. break;
  371. if (radix_tree_exceptional_entry(page)) {
  372. if (unfalloc)
  373. continue;
  374. nr_swaps_freed += !shmem_free_swap(mapping,
  375. index, page);
  376. continue;
  377. }
  378. if (!trylock_page(page))
  379. continue;
  380. if (!unfalloc || !PageUptodate(page)) {
  381. if (page->mapping == mapping) {
  382. VM_BUG_ON_PAGE(PageWriteback(page), page);
  383. truncate_inode_page(mapping, page);
  384. }
  385. }
  386. unlock_page(page);
  387. }
  388. pagevec_remove_exceptionals(&pvec);
  389. pagevec_release(&pvec);
  390. mem_cgroup_uncharge_end();
  391. cond_resched();
  392. index++;
  393. }
  394. if (partial_start) {
  395. struct page *page = NULL;
  396. shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
  397. if (page) {
  398. unsigned int top = PAGE_CACHE_SIZE;
  399. if (start > end) {
  400. top = partial_end;
  401. partial_end = 0;
  402. }
  403. zero_user_segment(page, partial_start, top);
  404. set_page_dirty(page);
  405. unlock_page(page);
  406. page_cache_release(page);
  407. }
  408. }
  409. if (partial_end) {
  410. struct page *page = NULL;
  411. shmem_getpage(inode, end, &page, SGP_READ, NULL);
  412. if (page) {
  413. zero_user_segment(page, 0, partial_end);
  414. set_page_dirty(page);
  415. unlock_page(page);
  416. page_cache_release(page);
  417. }
  418. }
  419. if (start >= end)
  420. return;
  421. index = start;
  422. for ( ; ; ) {
  423. cond_resched();
  424. pvec.nr = find_get_entries(mapping, index,
  425. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  426. pvec.pages, indices);
  427. if (!pvec.nr) {
  428. if (index == start || unfalloc)
  429. break;
  430. index = start;
  431. continue;
  432. }
  433. if ((index == start || unfalloc) && indices[0] >= end) {
  434. pagevec_remove_exceptionals(&pvec);
  435. pagevec_release(&pvec);
  436. break;
  437. }
  438. mem_cgroup_uncharge_start();
  439. for (i = 0; i < pagevec_count(&pvec); i++) {
  440. struct page *page = pvec.pages[i];
  441. index = indices[i];
  442. if (index >= end)
  443. break;
  444. if (radix_tree_exceptional_entry(page)) {
  445. if (unfalloc)
  446. continue;
  447. nr_swaps_freed += !shmem_free_swap(mapping,
  448. index, page);
  449. continue;
  450. }
  451. lock_page(page);
  452. if (!unfalloc || !PageUptodate(page)) {
  453. if (page->mapping == mapping) {
  454. VM_BUG_ON_PAGE(PageWriteback(page), page);
  455. truncate_inode_page(mapping, page);
  456. }
  457. }
  458. unlock_page(page);
  459. }
  460. pagevec_remove_exceptionals(&pvec);
  461. pagevec_release(&pvec);
  462. mem_cgroup_uncharge_end();
  463. index++;
  464. }
  465. spin_lock(&info->lock);
  466. info->swapped -= nr_swaps_freed;
  467. shmem_recalc_inode(inode);
  468. spin_unlock(&info->lock);
  469. }
  470. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  471. {
  472. shmem_undo_range(inode, lstart, lend, false);
  473. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  474. }
  475. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  476. static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
  477. {
  478. struct inode *inode = dentry->d_inode;
  479. int error;
  480. error = inode_change_ok(inode, attr);
  481. if (error)
  482. return error;
  483. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  484. loff_t oldsize = inode->i_size;
  485. loff_t newsize = attr->ia_size;
  486. if (newsize != oldsize) {
  487. i_size_write(inode, newsize);
  488. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  489. }
  490. if (newsize < oldsize) {
  491. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  492. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  493. shmem_truncate_range(inode, newsize, (loff_t)-1);
  494. /* unmap again to remove racily COWed private pages */
  495. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  496. }
  497. }
  498. setattr_copy(inode, attr);
  499. if (attr->ia_valid & ATTR_MODE)
  500. error = posix_acl_chmod(inode, inode->i_mode);
  501. return error;
  502. }
  503. static void shmem_evict_inode(struct inode *inode)
  504. {
  505. struct shmem_inode_info *info = SHMEM_I(inode);
  506. if (inode->i_mapping->a_ops == &shmem_aops) {
  507. shmem_unacct_size(info->flags, inode->i_size);
  508. inode->i_size = 0;
  509. shmem_truncate_range(inode, 0, (loff_t)-1);
  510. if (!list_empty(&info->swaplist)) {
  511. mutex_lock(&shmem_swaplist_mutex);
  512. list_del_init(&info->swaplist);
  513. mutex_unlock(&shmem_swaplist_mutex);
  514. }
  515. } else
  516. kfree(info->symlink);
  517. simple_xattrs_free(&info->xattrs);
  518. WARN_ON(inode->i_blocks);
  519. shmem_free_inode(inode->i_sb);
  520. clear_inode(inode);
  521. }
  522. /*
  523. * If swap found in inode, free it and move page from swapcache to filecache.
  524. */
  525. static int shmem_unuse_inode(struct shmem_inode_info *info,
  526. swp_entry_t swap, struct page **pagep)
  527. {
  528. struct address_space *mapping = info->vfs_inode.i_mapping;
  529. void *radswap;
  530. pgoff_t index;
  531. gfp_t gfp;
  532. int error = 0;
  533. radswap = swp_to_radix_entry(swap);
  534. index = radix_tree_locate_item(&mapping->page_tree, radswap);
  535. if (index == -1)
  536. return 0;
  537. /*
  538. * Move _head_ to start search for next from here.
  539. * But be careful: shmem_evict_inode checks list_empty without taking
  540. * mutex, and there's an instant in list_move_tail when info->swaplist
  541. * would appear empty, if it were the only one on shmem_swaplist.
  542. */
  543. if (shmem_swaplist.next != &info->swaplist)
  544. list_move_tail(&shmem_swaplist, &info->swaplist);
  545. gfp = mapping_gfp_mask(mapping);
  546. if (shmem_should_replace_page(*pagep, gfp)) {
  547. mutex_unlock(&shmem_swaplist_mutex);
  548. error = shmem_replace_page(pagep, gfp, info, index);
  549. mutex_lock(&shmem_swaplist_mutex);
  550. /*
  551. * We needed to drop mutex to make that restrictive page
  552. * allocation, but the inode might have been freed while we
  553. * dropped it: although a racing shmem_evict_inode() cannot
  554. * complete without emptying the radix_tree, our page lock
  555. * on this swapcache page is not enough to prevent that -
  556. * free_swap_and_cache() of our swap entry will only
  557. * trylock_page(), removing swap from radix_tree whatever.
  558. *
  559. * We must not proceed to shmem_add_to_page_cache() if the
  560. * inode has been freed, but of course we cannot rely on
  561. * inode or mapping or info to check that. However, we can
  562. * safely check if our swap entry is still in use (and here
  563. * it can't have got reused for another page): if it's still
  564. * in use, then the inode cannot have been freed yet, and we
  565. * can safely proceed (if it's no longer in use, that tells
  566. * nothing about the inode, but we don't need to unuse swap).
  567. */
  568. if (!page_swapcount(*pagep))
  569. error = -ENOENT;
  570. }
  571. /*
  572. * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
  573. * but also to hold up shmem_evict_inode(): so inode cannot be freed
  574. * beneath us (pagelock doesn't help until the page is in pagecache).
  575. */
  576. if (!error)
  577. error = shmem_add_to_page_cache(*pagep, mapping, index,
  578. GFP_NOWAIT, radswap);
  579. if (error != -ENOMEM) {
  580. /*
  581. * Truncation and eviction use free_swap_and_cache(), which
  582. * only does trylock page: if we raced, best clean up here.
  583. */
  584. delete_from_swap_cache(*pagep);
  585. set_page_dirty(*pagep);
  586. if (!error) {
  587. spin_lock(&info->lock);
  588. info->swapped--;
  589. spin_unlock(&info->lock);
  590. swap_free(swap);
  591. }
  592. error = 1; /* not an error, but entry was found */
  593. }
  594. return error;
  595. }
  596. /*
  597. * Search through swapped inodes to find and replace swap by page.
  598. */
  599. int shmem_unuse(swp_entry_t swap, struct page *page)
  600. {
  601. struct list_head *this, *next;
  602. struct shmem_inode_info *info;
  603. int found = 0;
  604. int error = 0;
  605. /*
  606. * There's a faint possibility that swap page was replaced before
  607. * caller locked it: caller will come back later with the right page.
  608. */
  609. if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
  610. goto out;
  611. /*
  612. * Charge page using GFP_KERNEL while we can wait, before taking
  613. * the shmem_swaplist_mutex which might hold up shmem_writepage().
  614. * Charged back to the user (not to caller) when swap account is used.
  615. */
  616. error = mem_cgroup_charge_file(page, current->mm, GFP_KERNEL);
  617. if (error)
  618. goto out;
  619. /* No radix_tree_preload: swap entry keeps a place for page in tree */
  620. mutex_lock(&shmem_swaplist_mutex);
  621. list_for_each_safe(this, next, &shmem_swaplist) {
  622. info = list_entry(this, struct shmem_inode_info, swaplist);
  623. if (info->swapped)
  624. found = shmem_unuse_inode(info, swap, &page);
  625. else
  626. list_del_init(&info->swaplist);
  627. cond_resched();
  628. if (found)
  629. break;
  630. }
  631. mutex_unlock(&shmem_swaplist_mutex);
  632. if (found < 0)
  633. error = found;
  634. out:
  635. unlock_page(page);
  636. page_cache_release(page);
  637. return error;
  638. }
  639. /*
  640. * Move the page from the page cache to the swap cache.
  641. */
  642. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  643. {
  644. struct shmem_inode_info *info;
  645. struct address_space *mapping;
  646. struct inode *inode;
  647. swp_entry_t swap;
  648. pgoff_t index;
  649. BUG_ON(!PageLocked(page));
  650. mapping = page->mapping;
  651. index = page->index;
  652. inode = mapping->host;
  653. info = SHMEM_I(inode);
  654. if (info->flags & VM_LOCKED)
  655. goto redirty;
  656. if (!total_swap_pages)
  657. goto redirty;
  658. /*
  659. * shmem_backing_dev_info's capabilities prevent regular writeback or
  660. * sync from ever calling shmem_writepage; but a stacking filesystem
  661. * might use ->writepage of its underlying filesystem, in which case
  662. * tmpfs should write out to swap only in response to memory pressure,
  663. * and not for the writeback threads or sync.
  664. */
  665. if (!wbc->for_reclaim) {
  666. WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
  667. goto redirty;
  668. }
  669. /*
  670. * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
  671. * value into swapfile.c, the only way we can correctly account for a
  672. * fallocated page arriving here is now to initialize it and write it.
  673. *
  674. * That's okay for a page already fallocated earlier, but if we have
  675. * not yet completed the fallocation, then (a) we want to keep track
  676. * of this page in case we have to undo it, and (b) it may not be a
  677. * good idea to continue anyway, once we're pushing into swap. So
  678. * reactivate the page, and let shmem_fallocate() quit when too many.
  679. */
  680. if (!PageUptodate(page)) {
  681. if (inode->i_private) {
  682. struct shmem_falloc *shmem_falloc;
  683. spin_lock(&inode->i_lock);
  684. shmem_falloc = inode->i_private;
  685. if (shmem_falloc &&
  686. index >= shmem_falloc->start &&
  687. index < shmem_falloc->next)
  688. shmem_falloc->nr_unswapped++;
  689. else
  690. shmem_falloc = NULL;
  691. spin_unlock(&inode->i_lock);
  692. if (shmem_falloc)
  693. goto redirty;
  694. }
  695. clear_highpage(page);
  696. flush_dcache_page(page);
  697. SetPageUptodate(page);
  698. }
  699. swap = get_swap_page();
  700. if (!swap.val)
  701. goto redirty;
  702. /*
  703. * Add inode to shmem_unuse()'s list of swapped-out inodes,
  704. * if it's not already there. Do it now before the page is
  705. * moved to swap cache, when its pagelock no longer protects
  706. * the inode from eviction. But don't unlock the mutex until
  707. * we've incremented swapped, because shmem_unuse_inode() will
  708. * prune a !swapped inode from the swaplist under this mutex.
  709. */
  710. mutex_lock(&shmem_swaplist_mutex);
  711. if (list_empty(&info->swaplist))
  712. list_add_tail(&info->swaplist, &shmem_swaplist);
  713. if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
  714. swap_shmem_alloc(swap);
  715. shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
  716. spin_lock(&info->lock);
  717. info->swapped++;
  718. shmem_recalc_inode(inode);
  719. spin_unlock(&info->lock);
  720. mutex_unlock(&shmem_swaplist_mutex);
  721. BUG_ON(page_mapped(page));
  722. swap_writepage(page, wbc);
  723. return 0;
  724. }
  725. mutex_unlock(&shmem_swaplist_mutex);
  726. swapcache_free(swap, NULL);
  727. redirty:
  728. set_page_dirty(page);
  729. if (wbc->for_reclaim)
  730. return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
  731. unlock_page(page);
  732. return 0;
  733. }
  734. #ifdef CONFIG_NUMA
  735. #ifdef CONFIG_TMPFS
  736. static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  737. {
  738. char buffer[64];
  739. if (!mpol || mpol->mode == MPOL_DEFAULT)
  740. return; /* show nothing */
  741. mpol_to_str(buffer, sizeof(buffer), mpol);
  742. seq_printf(seq, ",mpol=%s", buffer);
  743. }
  744. static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  745. {
  746. struct mempolicy *mpol = NULL;
  747. if (sbinfo->mpol) {
  748. spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
  749. mpol = sbinfo->mpol;
  750. mpol_get(mpol);
  751. spin_unlock(&sbinfo->stat_lock);
  752. }
  753. return mpol;
  754. }
  755. #endif /* CONFIG_TMPFS */
  756. static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  757. struct shmem_inode_info *info, pgoff_t index)
  758. {
  759. struct vm_area_struct pvma;
  760. struct page *page;
  761. /* Create a pseudo vma that just contains the policy */
  762. pvma.vm_start = 0;
  763. /* Bias interleave by inode number to distribute better across nodes */
  764. pvma.vm_pgoff = index + info->vfs_inode.i_ino;
  765. pvma.vm_ops = NULL;
  766. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  767. page = swapin_readahead(swap, gfp, &pvma, 0);
  768. /* Drop reference taken by mpol_shared_policy_lookup() */
  769. mpol_cond_put(pvma.vm_policy);
  770. return page;
  771. }
  772. static struct page *shmem_alloc_page(gfp_t gfp,
  773. struct shmem_inode_info *info, pgoff_t index)
  774. {
  775. struct vm_area_struct pvma;
  776. struct page *page;
  777. /* Create a pseudo vma that just contains the policy */
  778. pvma.vm_start = 0;
  779. /* Bias interleave by inode number to distribute better across nodes */
  780. pvma.vm_pgoff = index + info->vfs_inode.i_ino;
  781. pvma.vm_ops = NULL;
  782. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  783. page = alloc_page_vma(gfp, &pvma, 0);
  784. /* Drop reference taken by mpol_shared_policy_lookup() */
  785. mpol_cond_put(pvma.vm_policy);
  786. return page;
  787. }
  788. #else /* !CONFIG_NUMA */
  789. #ifdef CONFIG_TMPFS
  790. static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  791. {
  792. }
  793. #endif /* CONFIG_TMPFS */
  794. static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  795. struct shmem_inode_info *info, pgoff_t index)
  796. {
  797. return swapin_readahead(swap, gfp, NULL, 0);
  798. }
  799. static inline struct page *shmem_alloc_page(gfp_t gfp,
  800. struct shmem_inode_info *info, pgoff_t index)
  801. {
  802. return alloc_page(gfp);
  803. }
  804. #endif /* CONFIG_NUMA */
  805. #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
  806. static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  807. {
  808. return NULL;
  809. }
  810. #endif
  811. /*
  812. * When a page is moved from swapcache to shmem filecache (either by the
  813. * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
  814. * shmem_unuse_inode()), it may have been read in earlier from swap, in
  815. * ignorance of the mapping it belongs to. If that mapping has special
  816. * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
  817. * we may need to copy to a suitable page before moving to filecache.
  818. *
  819. * In a future release, this may well be extended to respect cpuset and
  820. * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
  821. * but for now it is a simple matter of zone.
  822. */
  823. static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
  824. {
  825. return page_zonenum(page) > gfp_zone(gfp);
  826. }
  827. static int shmem_replace_page(struct page **pagep, gfp_t gfp,
  828. struct shmem_inode_info *info, pgoff_t index)
  829. {
  830. struct page *oldpage, *newpage;
  831. struct address_space *swap_mapping;
  832. pgoff_t swap_index;
  833. int error;
  834. oldpage = *pagep;
  835. swap_index = page_private(oldpage);
  836. swap_mapping = page_mapping(oldpage);
  837. /*
  838. * We have arrived here because our zones are constrained, so don't
  839. * limit chance of success by further cpuset and node constraints.
  840. */
  841. gfp &= ~GFP_CONSTRAINT_MASK;
  842. newpage = shmem_alloc_page(gfp, info, index);
  843. if (!newpage)
  844. return -ENOMEM;
  845. page_cache_get(newpage);
  846. copy_highpage(newpage, oldpage);
  847. flush_dcache_page(newpage);
  848. __set_page_locked(newpage);
  849. SetPageUptodate(newpage);
  850. SetPageSwapBacked(newpage);
  851. set_page_private(newpage, swap_index);
  852. SetPageSwapCache(newpage);
  853. /*
  854. * Our caller will very soon move newpage out of swapcache, but it's
  855. * a nice clean interface for us to replace oldpage by newpage there.
  856. */
  857. spin_lock_irq(&swap_mapping->tree_lock);
  858. error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
  859. newpage);
  860. if (!error) {
  861. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  862. __dec_zone_page_state(oldpage, NR_FILE_PAGES);
  863. }
  864. spin_unlock_irq(&swap_mapping->tree_lock);
  865. if (unlikely(error)) {
  866. /*
  867. * Is this possible? I think not, now that our callers check
  868. * both PageSwapCache and page_private after getting page lock;
  869. * but be defensive. Reverse old to newpage for clear and free.
  870. */
  871. oldpage = newpage;
  872. } else {
  873. mem_cgroup_replace_page_cache(oldpage, newpage);
  874. lru_cache_add_anon(newpage);
  875. *pagep = newpage;
  876. }
  877. ClearPageSwapCache(oldpage);
  878. set_page_private(oldpage, 0);
  879. unlock_page(oldpage);
  880. page_cache_release(oldpage);
  881. page_cache_release(oldpage);
  882. return error;
  883. }
  884. /*
  885. * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
  886. *
  887. * If we allocate a new one we do not mark it dirty. That's up to the
  888. * vm. If we swap it in we mark it dirty since we also free the swap
  889. * entry since a page cannot live in both the swap and page cache
  890. */
  891. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  892. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
  893. {
  894. struct address_space *mapping = inode->i_mapping;
  895. struct shmem_inode_info *info;
  896. struct shmem_sb_info *sbinfo;
  897. struct page *page;
  898. swp_entry_t swap;
  899. int error;
  900. int once = 0;
  901. int alloced = 0;
  902. if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
  903. return -EFBIG;
  904. repeat:
  905. swap.val = 0;
  906. page = find_lock_entry(mapping, index);
  907. if (radix_tree_exceptional_entry(page)) {
  908. swap = radix_to_swp_entry(page);
  909. page = NULL;
  910. }
  911. if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
  912. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  913. error = -EINVAL;
  914. goto failed;
  915. }
  916. /* fallocated page? */
  917. if (page && !PageUptodate(page)) {
  918. if (sgp != SGP_READ)
  919. goto clear;
  920. unlock_page(page);
  921. page_cache_release(page);
  922. page = NULL;
  923. }
  924. if (page || (sgp == SGP_READ && !swap.val)) {
  925. *pagep = page;
  926. return 0;
  927. }
  928. /*
  929. * Fast cache lookup did not find it:
  930. * bring it back from swap or allocate.
  931. */
  932. info = SHMEM_I(inode);
  933. sbinfo = SHMEM_SB(inode->i_sb);
  934. if (swap.val) {
  935. /* Look it up and read it in.. */
  936. page = lookup_swap_cache(swap);
  937. if (!page) {
  938. /* here we actually do the io */
  939. if (fault_type)
  940. *fault_type |= VM_FAULT_MAJOR;
  941. page = shmem_swapin(swap, gfp, info, index);
  942. if (!page) {
  943. error = -ENOMEM;
  944. goto failed;
  945. }
  946. }
  947. /* We have to do this with page locked to prevent races */
  948. lock_page(page);
  949. if (!PageSwapCache(page) || page_private(page) != swap.val ||
  950. !shmem_confirm_swap(mapping, index, swap)) {
  951. error = -EEXIST; /* try again */
  952. goto unlock;
  953. }
  954. if (!PageUptodate(page)) {
  955. error = -EIO;
  956. goto failed;
  957. }
  958. wait_on_page_writeback(page);
  959. if (shmem_should_replace_page(page, gfp)) {
  960. error = shmem_replace_page(&page, gfp, info, index);
  961. if (error)
  962. goto failed;
  963. }
  964. error = mem_cgroup_charge_file(page, current->mm,
  965. gfp & GFP_RECLAIM_MASK);
  966. if (!error) {
  967. error = shmem_add_to_page_cache(page, mapping, index,
  968. gfp, swp_to_radix_entry(swap));
  969. /*
  970. * We already confirmed swap under page lock, and make
  971. * no memory allocation here, so usually no possibility
  972. * of error; but free_swap_and_cache() only trylocks a
  973. * page, so it is just possible that the entry has been
  974. * truncated or holepunched since swap was confirmed.
  975. * shmem_undo_range() will have done some of the
  976. * unaccounting, now delete_from_swap_cache() will do
  977. * the rest (including mem_cgroup_uncharge_swapcache).
  978. * Reset swap.val? No, leave it so "failed" goes back to
  979. * "repeat": reading a hole and writing should succeed.
  980. */
  981. if (error)
  982. delete_from_swap_cache(page);
  983. }
  984. if (error)
  985. goto failed;
  986. spin_lock(&info->lock);
  987. info->swapped--;
  988. shmem_recalc_inode(inode);
  989. spin_unlock(&info->lock);
  990. delete_from_swap_cache(page);
  991. set_page_dirty(page);
  992. swap_free(swap);
  993. } else {
  994. if (shmem_acct_block(info->flags)) {
  995. error = -ENOSPC;
  996. goto failed;
  997. }
  998. if (sbinfo->max_blocks) {
  999. if (percpu_counter_compare(&sbinfo->used_blocks,
  1000. sbinfo->max_blocks) >= 0) {
  1001. error = -ENOSPC;
  1002. goto unacct;
  1003. }
  1004. percpu_counter_inc(&sbinfo->used_blocks);
  1005. }
  1006. page = shmem_alloc_page(gfp, info, index);
  1007. if (!page) {
  1008. error = -ENOMEM;
  1009. goto decused;
  1010. }
  1011. SetPageSwapBacked(page);
  1012. __set_page_locked(page);
  1013. error = mem_cgroup_charge_file(page, current->mm,
  1014. gfp & GFP_RECLAIM_MASK);
  1015. if (error)
  1016. goto decused;
  1017. error = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK);
  1018. if (!error) {
  1019. error = shmem_add_to_page_cache(page, mapping, index,
  1020. gfp, NULL);
  1021. radix_tree_preload_end();
  1022. }
  1023. if (error) {
  1024. mem_cgroup_uncharge_cache_page(page);
  1025. goto decused;
  1026. }
  1027. lru_cache_add_anon(page);
  1028. spin_lock(&info->lock);
  1029. info->alloced++;
  1030. inode->i_blocks += BLOCKS_PER_PAGE;
  1031. shmem_recalc_inode(inode);
  1032. spin_unlock(&info->lock);
  1033. alloced = true;
  1034. /*
  1035. * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
  1036. */
  1037. if (sgp == SGP_FALLOC)
  1038. sgp = SGP_WRITE;
  1039. clear:
  1040. /*
  1041. * Let SGP_WRITE caller clear ends if write does not fill page;
  1042. * but SGP_FALLOC on a page fallocated earlier must initialize
  1043. * it now, lest undo on failure cancel our earlier guarantee.
  1044. */
  1045. if (sgp != SGP_WRITE) {
  1046. clear_highpage(page);
  1047. flush_dcache_page(page);
  1048. SetPageUptodate(page);
  1049. }
  1050. if (sgp == SGP_DIRTY)
  1051. set_page_dirty(page);
  1052. }
  1053. /* Perhaps the file has been truncated since we checked */
  1054. if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
  1055. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  1056. error = -EINVAL;
  1057. if (alloced)
  1058. goto trunc;
  1059. else
  1060. goto failed;
  1061. }
  1062. *pagep = page;
  1063. return 0;
  1064. /*
  1065. * Error recovery.
  1066. */
  1067. trunc:
  1068. info = SHMEM_I(inode);
  1069. ClearPageDirty(page);
  1070. delete_from_page_cache(page);
  1071. spin_lock(&info->lock);
  1072. info->alloced--;
  1073. inode->i_blocks -= BLOCKS_PER_PAGE;
  1074. spin_unlock(&info->lock);
  1075. decused:
  1076. sbinfo = SHMEM_SB(inode->i_sb);
  1077. if (sbinfo->max_blocks)
  1078. percpu_counter_add(&sbinfo->used_blocks, -1);
  1079. unacct:
  1080. shmem_unacct_blocks(info->flags, 1);
  1081. failed:
  1082. if (swap.val && error != -EINVAL &&
  1083. !shmem_confirm_swap(mapping, index, swap))
  1084. error = -EEXIST;
  1085. unlock:
  1086. if (page) {
  1087. unlock_page(page);
  1088. page_cache_release(page);
  1089. }
  1090. if (error == -ENOSPC && !once++) {
  1091. info = SHMEM_I(inode);
  1092. spin_lock(&info->lock);
  1093. shmem_recalc_inode(inode);
  1094. spin_unlock(&info->lock);
  1095. goto repeat;
  1096. }
  1097. if (error == -EEXIST) /* from above or from radix_tree_insert */
  1098. goto repeat;
  1099. return error;
  1100. }
  1101. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1102. {
  1103. struct inode *inode = file_inode(vma->vm_file);
  1104. int error;
  1105. int ret = VM_FAULT_LOCKED;
  1106. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
  1107. if (error)
  1108. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  1109. if (ret & VM_FAULT_MAJOR) {
  1110. count_vm_event(PGMAJFAULT);
  1111. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  1112. }
  1113. return ret;
  1114. }
  1115. #ifdef CONFIG_NUMA
  1116. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
  1117. {
  1118. struct inode *inode = file_inode(vma->vm_file);
  1119. return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
  1120. }
  1121. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  1122. unsigned long addr)
  1123. {
  1124. struct inode *inode = file_inode(vma->vm_file);
  1125. pgoff_t index;
  1126. index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1127. return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
  1128. }
  1129. #endif
  1130. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1131. {
  1132. struct inode *inode = file_inode(file);
  1133. struct shmem_inode_info *info = SHMEM_I(inode);
  1134. int retval = -ENOMEM;
  1135. spin_lock(&info->lock);
  1136. if (lock && !(info->flags & VM_LOCKED)) {
  1137. if (!user_shm_lock(inode->i_size, user))
  1138. goto out_nomem;
  1139. info->flags |= VM_LOCKED;
  1140. mapping_set_unevictable(file->f_mapping);
  1141. }
  1142. if (!lock && (info->flags & VM_LOCKED) && user) {
  1143. user_shm_unlock(inode->i_size, user);
  1144. info->flags &= ~VM_LOCKED;
  1145. mapping_clear_unevictable(file->f_mapping);
  1146. }
  1147. retval = 0;
  1148. out_nomem:
  1149. spin_unlock(&info->lock);
  1150. return retval;
  1151. }
  1152. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1153. {
  1154. file_accessed(file);
  1155. vma->vm_ops = &shmem_vm_ops;
  1156. return 0;
  1157. }
  1158. static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
  1159. umode_t mode, dev_t dev, unsigned long flags)
  1160. {
  1161. struct inode *inode;
  1162. struct shmem_inode_info *info;
  1163. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1164. if (shmem_reserve_inode(sb))
  1165. return NULL;
  1166. inode = new_inode(sb);
  1167. if (inode) {
  1168. inode->i_ino = get_next_ino();
  1169. inode_init_owner(inode, dir, mode);
  1170. inode->i_blocks = 0;
  1171. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1172. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1173. inode->i_generation = get_seconds();
  1174. info = SHMEM_I(inode);
  1175. memset(info, 0, (char *)inode - (char *)info);
  1176. spin_lock_init(&info->lock);
  1177. info->flags = flags & VM_NORESERVE;
  1178. INIT_LIST_HEAD(&info->swaplist);
  1179. simple_xattrs_init(&info->xattrs);
  1180. cache_no_acl(inode);
  1181. switch (mode & S_IFMT) {
  1182. default:
  1183. inode->i_op = &shmem_special_inode_operations;
  1184. init_special_inode(inode, mode, dev);
  1185. break;
  1186. case S_IFREG:
  1187. inode->i_mapping->a_ops = &shmem_aops;
  1188. inode->i_op = &shmem_inode_operations;
  1189. inode->i_fop = &shmem_file_operations;
  1190. mpol_shared_policy_init(&info->policy,
  1191. shmem_get_sbmpol(sbinfo));
  1192. break;
  1193. case S_IFDIR:
  1194. inc_nlink(inode);
  1195. /* Some things misbehave if size == 0 on a directory */
  1196. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1197. inode->i_op = &shmem_dir_inode_operations;
  1198. inode->i_fop = &simple_dir_operations;
  1199. break;
  1200. case S_IFLNK:
  1201. /*
  1202. * Must not load anything in the rbtree,
  1203. * mpol_free_shared_policy will not be called.
  1204. */
  1205. mpol_shared_policy_init(&info->policy, NULL);
  1206. break;
  1207. }
  1208. } else
  1209. shmem_free_inode(sb);
  1210. return inode;
  1211. }
  1212. bool shmem_mapping(struct address_space *mapping)
  1213. {
  1214. return mapping->backing_dev_info == &shmem_backing_dev_info;
  1215. }
  1216. #ifdef CONFIG_TMPFS
  1217. static const struct inode_operations shmem_symlink_inode_operations;
  1218. static const struct inode_operations shmem_short_symlink_operations;
  1219. #ifdef CONFIG_TMPFS_XATTR
  1220. static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
  1221. #else
  1222. #define shmem_initxattrs NULL
  1223. #endif
  1224. static int
  1225. shmem_write_begin(struct file *file, struct address_space *mapping,
  1226. loff_t pos, unsigned len, unsigned flags,
  1227. struct page **pagep, void **fsdata)
  1228. {
  1229. struct inode *inode = mapping->host;
  1230. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1231. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1232. }
  1233. static int
  1234. shmem_write_end(struct file *file, struct address_space *mapping,
  1235. loff_t pos, unsigned len, unsigned copied,
  1236. struct page *page, void *fsdata)
  1237. {
  1238. struct inode *inode = mapping->host;
  1239. if (pos + copied > inode->i_size)
  1240. i_size_write(inode, pos + copied);
  1241. if (!PageUptodate(page)) {
  1242. if (copied < PAGE_CACHE_SIZE) {
  1243. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  1244. zero_user_segments(page, 0, from,
  1245. from + copied, PAGE_CACHE_SIZE);
  1246. }
  1247. SetPageUptodate(page);
  1248. }
  1249. set_page_dirty(page);
  1250. unlock_page(page);
  1251. page_cache_release(page);
  1252. return copied;
  1253. }
  1254. static ssize_t shmem_file_aio_read(struct kiocb *iocb,
  1255. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  1256. {
  1257. struct file *file = iocb->ki_filp;
  1258. struct inode *inode = file_inode(file);
  1259. struct address_space *mapping = inode->i_mapping;
  1260. pgoff_t index;
  1261. unsigned long offset;
  1262. enum sgp_type sgp = SGP_READ;
  1263. int error = 0;
  1264. ssize_t retval;
  1265. size_t count;
  1266. loff_t *ppos = &iocb->ki_pos;
  1267. struct iov_iter iter;
  1268. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1269. if (retval)
  1270. return retval;
  1271. iov_iter_init(&iter, iov, nr_segs, count, 0);
  1272. /*
  1273. * Might this read be for a stacking filesystem? Then when reading
  1274. * holes of a sparse file, we actually need to allocate those pages,
  1275. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  1276. */
  1277. if (segment_eq(get_fs(), KERNEL_DS))
  1278. sgp = SGP_DIRTY;
  1279. index = *ppos >> PAGE_CACHE_SHIFT;
  1280. offset = *ppos & ~PAGE_CACHE_MASK;
  1281. for (;;) {
  1282. struct page *page = NULL;
  1283. pgoff_t end_index;
  1284. unsigned long nr, ret;
  1285. loff_t i_size = i_size_read(inode);
  1286. end_index = i_size >> PAGE_CACHE_SHIFT;
  1287. if (index > end_index)
  1288. break;
  1289. if (index == end_index) {
  1290. nr = i_size & ~PAGE_CACHE_MASK;
  1291. if (nr <= offset)
  1292. break;
  1293. }
  1294. error = shmem_getpage(inode, index, &page, sgp, NULL);
  1295. if (error) {
  1296. if (error == -EINVAL)
  1297. error = 0;
  1298. break;
  1299. }
  1300. if (page)
  1301. unlock_page(page);
  1302. /*
  1303. * We must evaluate after, since reads (unlike writes)
  1304. * are called without i_mutex protection against truncate
  1305. */
  1306. nr = PAGE_CACHE_SIZE;
  1307. i_size = i_size_read(inode);
  1308. end_index = i_size >> PAGE_CACHE_SHIFT;
  1309. if (index == end_index) {
  1310. nr = i_size & ~PAGE_CACHE_MASK;
  1311. if (nr <= offset) {
  1312. if (page)
  1313. page_cache_release(page);
  1314. break;
  1315. }
  1316. }
  1317. nr -= offset;
  1318. if (page) {
  1319. /*
  1320. * If users can be writing to this page using arbitrary
  1321. * virtual addresses, take care about potential aliasing
  1322. * before reading the page on the kernel side.
  1323. */
  1324. if (mapping_writably_mapped(mapping))
  1325. flush_dcache_page(page);
  1326. /*
  1327. * Mark the page accessed if we read the beginning.
  1328. */
  1329. if (!offset)
  1330. mark_page_accessed(page);
  1331. } else {
  1332. page = ZERO_PAGE(0);
  1333. page_cache_get(page);
  1334. }
  1335. /*
  1336. * Ok, we have the page, and it's up-to-date, so
  1337. * now we can copy it to user space...
  1338. */
  1339. ret = copy_page_to_iter(page, offset, nr, &iter);
  1340. retval += ret;
  1341. offset += ret;
  1342. index += offset >> PAGE_CACHE_SHIFT;
  1343. offset &= ~PAGE_CACHE_MASK;
  1344. page_cache_release(page);
  1345. if (!iov_iter_count(&iter))
  1346. break;
  1347. if (ret < nr) {
  1348. error = -EFAULT;
  1349. break;
  1350. }
  1351. cond_resched();
  1352. }
  1353. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1354. file_accessed(file);
  1355. return retval ? retval : error;
  1356. }
  1357. static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
  1358. struct pipe_inode_info *pipe, size_t len,
  1359. unsigned int flags)
  1360. {
  1361. struct address_space *mapping = in->f_mapping;
  1362. struct inode *inode = mapping->host;
  1363. unsigned int loff, nr_pages, req_pages;
  1364. struct page *pages[PIPE_DEF_BUFFERS];
  1365. struct partial_page partial[PIPE_DEF_BUFFERS];
  1366. struct page *page;
  1367. pgoff_t index, end_index;
  1368. loff_t isize, left;
  1369. int error, page_nr;
  1370. struct splice_pipe_desc spd = {
  1371. .pages = pages,
  1372. .partial = partial,
  1373. .nr_pages_max = PIPE_DEF_BUFFERS,
  1374. .flags = flags,
  1375. .ops = &page_cache_pipe_buf_ops,
  1376. .spd_release = spd_release_page,
  1377. };
  1378. isize = i_size_read(inode);
  1379. if (unlikely(*ppos >= isize))
  1380. return 0;
  1381. left = isize - *ppos;
  1382. if (unlikely(left < len))
  1383. len = left;
  1384. if (splice_grow_spd(pipe, &spd))
  1385. return -ENOMEM;
  1386. index = *ppos >> PAGE_CACHE_SHIFT;
  1387. loff = *ppos & ~PAGE_CACHE_MASK;
  1388. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1389. nr_pages = min(req_pages, spd.nr_pages_max);
  1390. spd.nr_pages = find_get_pages_contig(mapping, index,
  1391. nr_pages, spd.pages);
  1392. index += spd.nr_pages;
  1393. error = 0;
  1394. while (spd.nr_pages < nr_pages) {
  1395. error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
  1396. if (error)
  1397. break;
  1398. unlock_page(page);
  1399. spd.pages[spd.nr_pages++] = page;
  1400. index++;
  1401. }
  1402. index = *ppos >> PAGE_CACHE_SHIFT;
  1403. nr_pages = spd.nr_pages;
  1404. spd.nr_pages = 0;
  1405. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  1406. unsigned int this_len;
  1407. if (!len)
  1408. break;
  1409. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  1410. page = spd.pages[page_nr];
  1411. if (!PageUptodate(page) || page->mapping != mapping) {
  1412. error = shmem_getpage(inode, index, &page,
  1413. SGP_CACHE, NULL);
  1414. if (error)
  1415. break;
  1416. unlock_page(page);
  1417. page_cache_release(spd.pages[page_nr]);
  1418. spd.pages[page_nr] = page;
  1419. }
  1420. isize = i_size_read(inode);
  1421. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1422. if (unlikely(!isize || index > end_index))
  1423. break;
  1424. if (end_index == index) {
  1425. unsigned int plen;
  1426. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1427. if (plen <= loff)
  1428. break;
  1429. this_len = min(this_len, plen - loff);
  1430. len = this_len;
  1431. }
  1432. spd.partial[page_nr].offset = loff;
  1433. spd.partial[page_nr].len = this_len;
  1434. len -= this_len;
  1435. loff = 0;
  1436. spd.nr_pages++;
  1437. index++;
  1438. }
  1439. while (page_nr < nr_pages)
  1440. page_cache_release(spd.pages[page_nr++]);
  1441. if (spd.nr_pages)
  1442. error = splice_to_pipe(pipe, &spd);
  1443. splice_shrink_spd(&spd);
  1444. if (error > 0) {
  1445. *ppos += error;
  1446. file_accessed(in);
  1447. }
  1448. return error;
  1449. }
  1450. /*
  1451. * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
  1452. */
  1453. static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
  1454. pgoff_t index, pgoff_t end, int whence)
  1455. {
  1456. struct page *page;
  1457. struct pagevec pvec;
  1458. pgoff_t indices[PAGEVEC_SIZE];
  1459. bool done = false;
  1460. int i;
  1461. pagevec_init(&pvec, 0);
  1462. pvec.nr = 1; /* start small: we may be there already */
  1463. while (!done) {
  1464. pvec.nr = find_get_entries(mapping, index,
  1465. pvec.nr, pvec.pages, indices);
  1466. if (!pvec.nr) {
  1467. if (whence == SEEK_DATA)
  1468. index = end;
  1469. break;
  1470. }
  1471. for (i = 0; i < pvec.nr; i++, index++) {
  1472. if (index < indices[i]) {
  1473. if (whence == SEEK_HOLE) {
  1474. done = true;
  1475. break;
  1476. }
  1477. index = indices[i];
  1478. }
  1479. page = pvec.pages[i];
  1480. if (page && !radix_tree_exceptional_entry(page)) {
  1481. if (!PageUptodate(page))
  1482. page = NULL;
  1483. }
  1484. if (index >= end ||
  1485. (page && whence == SEEK_DATA) ||
  1486. (!page && whence == SEEK_HOLE)) {
  1487. done = true;
  1488. break;
  1489. }
  1490. }
  1491. pagevec_remove_exceptionals(&pvec);
  1492. pagevec_release(&pvec);
  1493. pvec.nr = PAGEVEC_SIZE;
  1494. cond_resched();
  1495. }
  1496. return index;
  1497. }
  1498. static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
  1499. {
  1500. struct address_space *mapping = file->f_mapping;
  1501. struct inode *inode = mapping->host;
  1502. pgoff_t start, end;
  1503. loff_t new_offset;
  1504. if (whence != SEEK_DATA && whence != SEEK_HOLE)
  1505. return generic_file_llseek_size(file, offset, whence,
  1506. MAX_LFS_FILESIZE, i_size_read(inode));
  1507. mutex_lock(&inode->i_mutex);
  1508. /* We're holding i_mutex so we can access i_size directly */
  1509. if (offset < 0)
  1510. offset = -EINVAL;
  1511. else if (offset >= inode->i_size)
  1512. offset = -ENXIO;
  1513. else {
  1514. start = offset >> PAGE_CACHE_SHIFT;
  1515. end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1516. new_offset = shmem_seek_hole_data(mapping, start, end, whence);
  1517. new_offset <<= PAGE_CACHE_SHIFT;
  1518. if (new_offset > offset) {
  1519. if (new_offset < inode->i_size)
  1520. offset = new_offset;
  1521. else if (whence == SEEK_DATA)
  1522. offset = -ENXIO;
  1523. else
  1524. offset = inode->i_size;
  1525. }
  1526. }
  1527. if (offset >= 0)
  1528. offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
  1529. mutex_unlock(&inode->i_mutex);
  1530. return offset;
  1531. }
  1532. static long shmem_fallocate(struct file *file, int mode, loff_t offset,
  1533. loff_t len)
  1534. {
  1535. struct inode *inode = file_inode(file);
  1536. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1537. struct shmem_falloc shmem_falloc;
  1538. pgoff_t start, index, end;
  1539. int error;
  1540. mutex_lock(&inode->i_mutex);
  1541. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1542. struct address_space *mapping = file->f_mapping;
  1543. loff_t unmap_start = round_up(offset, PAGE_SIZE);
  1544. loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
  1545. if ((u64)unmap_end > (u64)unmap_start)
  1546. unmap_mapping_range(mapping, unmap_start,
  1547. 1 + unmap_end - unmap_start, 0);
  1548. shmem_truncate_range(inode, offset, offset + len - 1);
  1549. /* No need to unmap again: hole-punching leaves COWed pages */
  1550. error = 0;
  1551. goto out;
  1552. }
  1553. /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
  1554. error = inode_newsize_ok(inode, offset + len);
  1555. if (error)
  1556. goto out;
  1557. start = offset >> PAGE_CACHE_SHIFT;
  1558. end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1559. /* Try to avoid a swapstorm if len is impossible to satisfy */
  1560. if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
  1561. error = -ENOSPC;
  1562. goto out;
  1563. }
  1564. shmem_falloc.start = start;
  1565. shmem_falloc.next = start;
  1566. shmem_falloc.nr_falloced = 0;
  1567. shmem_falloc.nr_unswapped = 0;
  1568. spin_lock(&inode->i_lock);
  1569. inode->i_private = &shmem_falloc;
  1570. spin_unlock(&inode->i_lock);
  1571. for (index = start; index < end; index++) {
  1572. struct page *page;
  1573. /*
  1574. * Good, the fallocate(2) manpage permits EINTR: we may have
  1575. * been interrupted because we are using up too much memory.
  1576. */
  1577. if (signal_pending(current))
  1578. error = -EINTR;
  1579. else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
  1580. error = -ENOMEM;
  1581. else
  1582. error = shmem_getpage(inode, index, &page, SGP_FALLOC,
  1583. NULL);
  1584. if (error) {
  1585. /* Remove the !PageUptodate pages we added */
  1586. shmem_undo_range(inode,
  1587. (loff_t)start << PAGE_CACHE_SHIFT,
  1588. (loff_t)index << PAGE_CACHE_SHIFT, true);
  1589. goto undone;
  1590. }
  1591. /*
  1592. * Inform shmem_writepage() how far we have reached.
  1593. * No need for lock or barrier: we have the page lock.
  1594. */
  1595. shmem_falloc.next++;
  1596. if (!PageUptodate(page))
  1597. shmem_falloc.nr_falloced++;
  1598. /*
  1599. * If !PageUptodate, leave it that way so that freeable pages
  1600. * can be recognized if we need to rollback on error later.
  1601. * But set_page_dirty so that memory pressure will swap rather
  1602. * than free the pages we are allocating (and SGP_CACHE pages
  1603. * might still be clean: we now need to mark those dirty too).
  1604. */
  1605. set_page_dirty(page);
  1606. unlock_page(page);
  1607. page_cache_release(page);
  1608. cond_resched();
  1609. }
  1610. if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
  1611. i_size_write(inode, offset + len);
  1612. inode->i_ctime = CURRENT_TIME;
  1613. undone:
  1614. spin_lock(&inode->i_lock);
  1615. inode->i_private = NULL;
  1616. spin_unlock(&inode->i_lock);
  1617. out:
  1618. mutex_unlock(&inode->i_mutex);
  1619. return error;
  1620. }
  1621. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1622. {
  1623. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1624. buf->f_type = TMPFS_MAGIC;
  1625. buf->f_bsize = PAGE_CACHE_SIZE;
  1626. buf->f_namelen = NAME_MAX;
  1627. if (sbinfo->max_blocks) {
  1628. buf->f_blocks = sbinfo->max_blocks;
  1629. buf->f_bavail =
  1630. buf->f_bfree = sbinfo->max_blocks -
  1631. percpu_counter_sum(&sbinfo->used_blocks);
  1632. }
  1633. if (sbinfo->max_inodes) {
  1634. buf->f_files = sbinfo->max_inodes;
  1635. buf->f_ffree = sbinfo->free_inodes;
  1636. }
  1637. /* else leave those fields 0 like simple_statfs */
  1638. return 0;
  1639. }
  1640. /*
  1641. * File creation. Allocate an inode, and we're done..
  1642. */
  1643. static int
  1644. shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  1645. {
  1646. struct inode *inode;
  1647. int error = -ENOSPC;
  1648. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
  1649. if (inode) {
  1650. error = simple_acl_create(dir, inode);
  1651. if (error)
  1652. goto out_iput;
  1653. error = security_inode_init_security(inode, dir,
  1654. &dentry->d_name,
  1655. shmem_initxattrs, NULL);
  1656. if (error && error != -EOPNOTSUPP)
  1657. goto out_iput;
  1658. error = 0;
  1659. dir->i_size += BOGO_DIRENT_SIZE;
  1660. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1661. d_instantiate(dentry, inode);
  1662. dget(dentry); /* Extra count - pin the dentry in core */
  1663. }
  1664. return error;
  1665. out_iput:
  1666. iput(inode);
  1667. return error;
  1668. }
  1669. static int
  1670. shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  1671. {
  1672. struct inode *inode;
  1673. int error = -ENOSPC;
  1674. inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
  1675. if (inode) {
  1676. error = security_inode_init_security(inode, dir,
  1677. NULL,
  1678. shmem_initxattrs, NULL);
  1679. if (error && error != -EOPNOTSUPP)
  1680. goto out_iput;
  1681. error = simple_acl_create(dir, inode);
  1682. if (error)
  1683. goto out_iput;
  1684. d_tmpfile(dentry, inode);
  1685. }
  1686. return error;
  1687. out_iput:
  1688. iput(inode);
  1689. return error;
  1690. }
  1691. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1692. {
  1693. int error;
  1694. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1695. return error;
  1696. inc_nlink(dir);
  1697. return 0;
  1698. }
  1699. static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  1700. bool excl)
  1701. {
  1702. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1703. }
  1704. /*
  1705. * Link a file..
  1706. */
  1707. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1708. {
  1709. struct inode *inode = old_dentry->d_inode;
  1710. int ret;
  1711. /*
  1712. * No ordinary (disk based) filesystem counts links as inodes;
  1713. * but each new link needs a new dentry, pinning lowmem, and
  1714. * tmpfs dentries cannot be pruned until they are unlinked.
  1715. */
  1716. ret = shmem_reserve_inode(inode->i_sb);
  1717. if (ret)
  1718. goto out;
  1719. dir->i_size += BOGO_DIRENT_SIZE;
  1720. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1721. inc_nlink(inode);
  1722. ihold(inode); /* New dentry reference */
  1723. dget(dentry); /* Extra pinning count for the created dentry */
  1724. d_instantiate(dentry, inode);
  1725. out:
  1726. return ret;
  1727. }
  1728. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1729. {
  1730. struct inode *inode = dentry->d_inode;
  1731. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  1732. shmem_free_inode(inode->i_sb);
  1733. dir->i_size -= BOGO_DIRENT_SIZE;
  1734. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1735. drop_nlink(inode);
  1736. dput(dentry); /* Undo the count from "create" - this does all the work */
  1737. return 0;
  1738. }
  1739. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1740. {
  1741. if (!simple_empty(dentry))
  1742. return -ENOTEMPTY;
  1743. drop_nlink(dentry->d_inode);
  1744. drop_nlink(dir);
  1745. return shmem_unlink(dir, dentry);
  1746. }
  1747. /*
  1748. * The VFS layer already does all the dentry stuff for rename,
  1749. * we just have to decrement the usage count for the target if
  1750. * it exists so that the VFS layer correctly free's it when it
  1751. * gets overwritten.
  1752. */
  1753. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1754. {
  1755. struct inode *inode = old_dentry->d_inode;
  1756. int they_are_dirs = S_ISDIR(inode->i_mode);
  1757. if (!simple_empty(new_dentry))
  1758. return -ENOTEMPTY;
  1759. if (new_dentry->d_inode) {
  1760. (void) shmem_unlink(new_dir, new_dentry);
  1761. if (they_are_dirs)
  1762. drop_nlink(old_dir);
  1763. } else if (they_are_dirs) {
  1764. drop_nlink(old_dir);
  1765. inc_nlink(new_dir);
  1766. }
  1767. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1768. new_dir->i_size += BOGO_DIRENT_SIZE;
  1769. old_dir->i_ctime = old_dir->i_mtime =
  1770. new_dir->i_ctime = new_dir->i_mtime =
  1771. inode->i_ctime = CURRENT_TIME;
  1772. return 0;
  1773. }
  1774. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1775. {
  1776. int error;
  1777. int len;
  1778. struct inode *inode;
  1779. struct page *page;
  1780. char *kaddr;
  1781. struct shmem_inode_info *info;
  1782. len = strlen(symname) + 1;
  1783. if (len > PAGE_CACHE_SIZE)
  1784. return -ENAMETOOLONG;
  1785. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
  1786. if (!inode)
  1787. return -ENOSPC;
  1788. error = security_inode_init_security(inode, dir, &dentry->d_name,
  1789. shmem_initxattrs, NULL);
  1790. if (error) {
  1791. if (error != -EOPNOTSUPP) {
  1792. iput(inode);
  1793. return error;
  1794. }
  1795. error = 0;
  1796. }
  1797. info = SHMEM_I(inode);
  1798. inode->i_size = len-1;
  1799. if (len <= SHORT_SYMLINK_LEN) {
  1800. info->symlink = kmemdup(symname, len, GFP_KERNEL);
  1801. if (!info->symlink) {
  1802. iput(inode);
  1803. return -ENOMEM;
  1804. }
  1805. inode->i_op = &shmem_short_symlink_operations;
  1806. } else {
  1807. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1808. if (error) {
  1809. iput(inode);
  1810. return error;
  1811. }
  1812. inode->i_mapping->a_ops = &shmem_aops;
  1813. inode->i_op = &shmem_symlink_inode_operations;
  1814. kaddr = kmap_atomic(page);
  1815. memcpy(kaddr, symname, len);
  1816. kunmap_atomic(kaddr);
  1817. SetPageUptodate(page);
  1818. set_page_dirty(page);
  1819. unlock_page(page);
  1820. page_cache_release(page);
  1821. }
  1822. dir->i_size += BOGO_DIRENT_SIZE;
  1823. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1824. d_instantiate(dentry, inode);
  1825. dget(dentry);
  1826. return 0;
  1827. }
  1828. static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
  1829. {
  1830. nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
  1831. return NULL;
  1832. }
  1833. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1834. {
  1835. struct page *page = NULL;
  1836. int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1837. nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
  1838. if (page)
  1839. unlock_page(page);
  1840. return page;
  1841. }
  1842. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1843. {
  1844. if (!IS_ERR(nd_get_link(nd))) {
  1845. struct page *page = cookie;
  1846. kunmap(page);
  1847. mark_page_accessed(page);
  1848. page_cache_release(page);
  1849. }
  1850. }
  1851. #ifdef CONFIG_TMPFS_XATTR
  1852. /*
  1853. * Superblocks without xattr inode operations may get some security.* xattr
  1854. * support from the LSM "for free". As soon as we have any other xattrs
  1855. * like ACLs, we also need to implement the security.* handlers at
  1856. * filesystem level, though.
  1857. */
  1858. /*
  1859. * Callback for security_inode_init_security() for acquiring xattrs.
  1860. */
  1861. static int shmem_initxattrs(struct inode *inode,
  1862. const struct xattr *xattr_array,
  1863. void *fs_info)
  1864. {
  1865. struct shmem_inode_info *info = SHMEM_I(inode);
  1866. const struct xattr *xattr;
  1867. struct simple_xattr *new_xattr;
  1868. size_t len;
  1869. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  1870. new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
  1871. if (!new_xattr)
  1872. return -ENOMEM;
  1873. len = strlen(xattr->name) + 1;
  1874. new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
  1875. GFP_KERNEL);
  1876. if (!new_xattr->name) {
  1877. kfree(new_xattr);
  1878. return -ENOMEM;
  1879. }
  1880. memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
  1881. XATTR_SECURITY_PREFIX_LEN);
  1882. memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
  1883. xattr->name, len);
  1884. simple_xattr_list_add(&info->xattrs, new_xattr);
  1885. }
  1886. return 0;
  1887. }
  1888. static const struct xattr_handler *shmem_xattr_handlers[] = {
  1889. #ifdef CONFIG_TMPFS_POSIX_ACL
  1890. &posix_acl_access_xattr_handler,
  1891. &posix_acl_default_xattr_handler,
  1892. #endif
  1893. NULL
  1894. };
  1895. static int shmem_xattr_validate(const char *name)
  1896. {
  1897. struct { const char *prefix; size_t len; } arr[] = {
  1898. { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
  1899. { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
  1900. };
  1901. int i;
  1902. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1903. size_t preflen = arr[i].len;
  1904. if (strncmp(name, arr[i].prefix, preflen) == 0) {
  1905. if (!name[preflen])
  1906. return -EINVAL;
  1907. return 0;
  1908. }
  1909. }
  1910. return -EOPNOTSUPP;
  1911. }
  1912. static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
  1913. void *buffer, size_t size)
  1914. {
  1915. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  1916. int err;
  1917. /*
  1918. * If this is a request for a synthetic attribute in the system.*
  1919. * namespace use the generic infrastructure to resolve a handler
  1920. * for it via sb->s_xattr.
  1921. */
  1922. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1923. return generic_getxattr(dentry, name, buffer, size);
  1924. err = shmem_xattr_validate(name);
  1925. if (err)
  1926. return err;
  1927. return simple_xattr_get(&info->xattrs, name, buffer, size);
  1928. }
  1929. static int shmem_setxattr(struct dentry *dentry, const char *name,
  1930. const void *value, size_t size, int flags)
  1931. {
  1932. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  1933. int err;
  1934. /*
  1935. * If this is a request for a synthetic attribute in the system.*
  1936. * namespace use the generic infrastructure to resolve a handler
  1937. * for it via sb->s_xattr.
  1938. */
  1939. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1940. return generic_setxattr(dentry, name, value, size, flags);
  1941. err = shmem_xattr_validate(name);
  1942. if (err)
  1943. return err;
  1944. return simple_xattr_set(&info->xattrs, name, value, size, flags);
  1945. }
  1946. static int shmem_removexattr(struct dentry *dentry, const char *name)
  1947. {
  1948. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  1949. int err;
  1950. /*
  1951. * If this is a request for a synthetic attribute in the system.*
  1952. * namespace use the generic infrastructure to resolve a handler
  1953. * for it via sb->s_xattr.
  1954. */
  1955. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1956. return generic_removexattr(dentry, name);
  1957. err = shmem_xattr_validate(name);
  1958. if (err)
  1959. return err;
  1960. return simple_xattr_remove(&info->xattrs, name);
  1961. }
  1962. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  1963. {
  1964. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  1965. return simple_xattr_list(&info->xattrs, buffer, size);
  1966. }
  1967. #endif /* CONFIG_TMPFS_XATTR */
  1968. static const struct inode_operations shmem_short_symlink_operations = {
  1969. .readlink = generic_readlink,
  1970. .follow_link = shmem_follow_short_symlink,
  1971. #ifdef CONFIG_TMPFS_XATTR
  1972. .setxattr = shmem_setxattr,
  1973. .getxattr = shmem_getxattr,
  1974. .listxattr = shmem_listxattr,
  1975. .removexattr = shmem_removexattr,
  1976. #endif
  1977. };
  1978. static const struct inode_operations shmem_symlink_inode_operations = {
  1979. .readlink = generic_readlink,
  1980. .follow_link = shmem_follow_link,
  1981. .put_link = shmem_put_link,
  1982. #ifdef CONFIG_TMPFS_XATTR
  1983. .setxattr = shmem_setxattr,
  1984. .getxattr = shmem_getxattr,
  1985. .listxattr = shmem_listxattr,
  1986. .removexattr = shmem_removexattr,
  1987. #endif
  1988. };
  1989. static struct dentry *shmem_get_parent(struct dentry *child)
  1990. {
  1991. return ERR_PTR(-ESTALE);
  1992. }
  1993. static int shmem_match(struct inode *ino, void *vfh)
  1994. {
  1995. __u32 *fh = vfh;
  1996. __u64 inum = fh[2];
  1997. inum = (inum << 32) | fh[1];
  1998. return ino->i_ino == inum && fh[0] == ino->i_generation;
  1999. }
  2000. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  2001. struct fid *fid, int fh_len, int fh_type)
  2002. {
  2003. struct inode *inode;
  2004. struct dentry *dentry = NULL;
  2005. u64 inum;
  2006. if (fh_len < 3)
  2007. return NULL;
  2008. inum = fid->raw[2];
  2009. inum = (inum << 32) | fid->raw[1];
  2010. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  2011. shmem_match, fid->raw);
  2012. if (inode) {
  2013. dentry = d_find_alias(inode);
  2014. iput(inode);
  2015. }
  2016. return dentry;
  2017. }
  2018. static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
  2019. struct inode *parent)
  2020. {
  2021. if (*len < 3) {
  2022. *len = 3;
  2023. return FILEID_INVALID;
  2024. }
  2025. if (inode_unhashed(inode)) {
  2026. /* Unfortunately insert_inode_hash is not idempotent,
  2027. * so as we hash inodes here rather than at creation
  2028. * time, we need a lock to ensure we only try
  2029. * to do it once
  2030. */
  2031. static DEFINE_SPINLOCK(lock);
  2032. spin_lock(&lock);
  2033. if (inode_unhashed(inode))
  2034. __insert_inode_hash(inode,
  2035. inode->i_ino + inode->i_generation);
  2036. spin_unlock(&lock);
  2037. }
  2038. fh[0] = inode->i_generation;
  2039. fh[1] = inode->i_ino;
  2040. fh[2] = ((__u64)inode->i_ino) >> 32;
  2041. *len = 3;
  2042. return 1;
  2043. }
  2044. static const struct export_operations shmem_export_ops = {
  2045. .get_parent = shmem_get_parent,
  2046. .encode_fh = shmem_encode_fh,
  2047. .fh_to_dentry = shmem_fh_to_dentry,
  2048. };
  2049. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  2050. bool remount)
  2051. {
  2052. char *this_char, *value, *rest;
  2053. struct mempolicy *mpol = NULL;
  2054. uid_t uid;
  2055. gid_t gid;
  2056. while (options != NULL) {
  2057. this_char = options;
  2058. for (;;) {
  2059. /*
  2060. * NUL-terminate this option: unfortunately,
  2061. * mount options form a comma-separated list,
  2062. * but mpol's nodelist may also contain commas.
  2063. */
  2064. options = strchr(options, ',');
  2065. if (options == NULL)
  2066. break;
  2067. options++;
  2068. if (!isdigit(*options)) {
  2069. options[-1] = '\0';
  2070. break;
  2071. }
  2072. }
  2073. if (!*this_char)
  2074. continue;
  2075. if ((value = strchr(this_char,'=')) != NULL) {
  2076. *value++ = 0;
  2077. } else {
  2078. printk(KERN_ERR
  2079. "tmpfs: No value for mount option '%s'\n",
  2080. this_char);
  2081. goto error;
  2082. }
  2083. if (!strcmp(this_char,"size")) {
  2084. unsigned long long size;
  2085. size = memparse(value,&rest);
  2086. if (*rest == '%') {
  2087. size <<= PAGE_SHIFT;
  2088. size *= totalram_pages;
  2089. do_div(size, 100);
  2090. rest++;
  2091. }
  2092. if (*rest)
  2093. goto bad_val;
  2094. sbinfo->max_blocks =
  2095. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  2096. } else if (!strcmp(this_char,"nr_blocks")) {
  2097. sbinfo->max_blocks = memparse(value, &rest);
  2098. if (*rest)
  2099. goto bad_val;
  2100. } else if (!strcmp(this_char,"nr_inodes")) {
  2101. sbinfo->max_inodes = memparse(value, &rest);
  2102. if (*rest)
  2103. goto bad_val;
  2104. } else if (!strcmp(this_char,"mode")) {
  2105. if (remount)
  2106. continue;
  2107. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  2108. if (*rest)
  2109. goto bad_val;
  2110. } else if (!strcmp(this_char,"uid")) {
  2111. if (remount)
  2112. continue;
  2113. uid = simple_strtoul(value, &rest, 0);
  2114. if (*rest)
  2115. goto bad_val;
  2116. sbinfo->uid = make_kuid(current_user_ns(), uid);
  2117. if (!uid_valid(sbinfo->uid))
  2118. goto bad_val;
  2119. } else if (!strcmp(this_char,"gid")) {
  2120. if (remount)
  2121. continue;
  2122. gid = simple_strtoul(value, &rest, 0);
  2123. if (*rest)
  2124. goto bad_val;
  2125. sbinfo->gid = make_kgid(current_user_ns(), gid);
  2126. if (!gid_valid(sbinfo->gid))
  2127. goto bad_val;
  2128. } else if (!strcmp(this_char,"mpol")) {
  2129. mpol_put(mpol);
  2130. mpol = NULL;
  2131. if (mpol_parse_str(value, &mpol))
  2132. goto bad_val;
  2133. } else {
  2134. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  2135. this_char);
  2136. goto error;
  2137. }
  2138. }
  2139. sbinfo->mpol = mpol;
  2140. return 0;
  2141. bad_val:
  2142. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  2143. value, this_char);
  2144. error:
  2145. mpol_put(mpol);
  2146. return 1;
  2147. }
  2148. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  2149. {
  2150. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2151. struct shmem_sb_info config = *sbinfo;
  2152. unsigned long inodes;
  2153. int error = -EINVAL;
  2154. config.mpol = NULL;
  2155. if (shmem_parse_options(data, &config, true))
  2156. return error;
  2157. spin_lock(&sbinfo->stat_lock);
  2158. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  2159. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  2160. goto out;
  2161. if (config.max_inodes < inodes)
  2162. goto out;
  2163. /*
  2164. * Those tests disallow limited->unlimited while any are in use;
  2165. * but we must separately disallow unlimited->limited, because
  2166. * in that case we have no record of how much is already in use.
  2167. */
  2168. if (config.max_blocks && !sbinfo->max_blocks)
  2169. goto out;
  2170. if (config.max_inodes && !sbinfo->max_inodes)
  2171. goto out;
  2172. error = 0;
  2173. sbinfo->max_blocks = config.max_blocks;
  2174. sbinfo->max_inodes = config.max_inodes;
  2175. sbinfo->free_inodes = config.max_inodes - inodes;
  2176. /*
  2177. * Preserve previous mempolicy unless mpol remount option was specified.
  2178. */
  2179. if (config.mpol) {
  2180. mpol_put(sbinfo->mpol);
  2181. sbinfo->mpol = config.mpol; /* transfers initial ref */
  2182. }
  2183. out:
  2184. spin_unlock(&sbinfo->stat_lock);
  2185. return error;
  2186. }
  2187. static int shmem_show_options(struct seq_file *seq, struct dentry *root)
  2188. {
  2189. struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
  2190. if (sbinfo->max_blocks != shmem_default_max_blocks())
  2191. seq_printf(seq, ",size=%luk",
  2192. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  2193. if (sbinfo->max_inodes != shmem_default_max_inodes())
  2194. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  2195. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  2196. seq_printf(seq, ",mode=%03ho", sbinfo->mode);
  2197. if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
  2198. seq_printf(seq, ",uid=%u",
  2199. from_kuid_munged(&init_user_ns, sbinfo->uid));
  2200. if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
  2201. seq_printf(seq, ",gid=%u",
  2202. from_kgid_munged(&init_user_ns, sbinfo->gid));
  2203. shmem_show_mpol(seq, sbinfo->mpol);
  2204. return 0;
  2205. }
  2206. #endif /* CONFIG_TMPFS */
  2207. static void shmem_put_super(struct super_block *sb)
  2208. {
  2209. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2210. percpu_counter_destroy(&sbinfo->used_blocks);
  2211. mpol_put(sbinfo->mpol);
  2212. kfree(sbinfo);
  2213. sb->s_fs_info = NULL;
  2214. }
  2215. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  2216. {
  2217. struct inode *inode;
  2218. struct shmem_sb_info *sbinfo;
  2219. int err = -ENOMEM;
  2220. /* Round up to L1_CACHE_BYTES to resist false sharing */
  2221. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  2222. L1_CACHE_BYTES), GFP_KERNEL);
  2223. if (!sbinfo)
  2224. return -ENOMEM;
  2225. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  2226. sbinfo->uid = current_fsuid();
  2227. sbinfo->gid = current_fsgid();
  2228. sb->s_fs_info = sbinfo;
  2229. #ifdef CONFIG_TMPFS
  2230. /*
  2231. * Per default we only allow half of the physical ram per
  2232. * tmpfs instance, limiting inodes to one per page of lowmem;
  2233. * but the internal instance is left unlimited.
  2234. */
  2235. if (!(sb->s_flags & MS_KERNMOUNT)) {
  2236. sbinfo->max_blocks = shmem_default_max_blocks();
  2237. sbinfo->max_inodes = shmem_default_max_inodes();
  2238. if (shmem_parse_options(data, sbinfo, false)) {
  2239. err = -EINVAL;
  2240. goto failed;
  2241. }
  2242. } else {
  2243. sb->s_flags |= MS_NOUSER;
  2244. }
  2245. sb->s_export_op = &shmem_export_ops;
  2246. sb->s_flags |= MS_NOSEC;
  2247. #else
  2248. sb->s_flags |= MS_NOUSER;
  2249. #endif
  2250. spin_lock_init(&sbinfo->stat_lock);
  2251. if (percpu_counter_init(&sbinfo->used_blocks, 0))
  2252. goto failed;
  2253. sbinfo->free_inodes = sbinfo->max_inodes;
  2254. sb->s_maxbytes = MAX_LFS_FILESIZE;
  2255. sb->s_blocksize = PAGE_CACHE_SIZE;
  2256. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  2257. sb->s_magic = TMPFS_MAGIC;
  2258. sb->s_op = &shmem_ops;
  2259. sb->s_time_gran = 1;
  2260. #ifdef CONFIG_TMPFS_XATTR
  2261. sb->s_xattr = shmem_xattr_handlers;
  2262. #endif
  2263. #ifdef CONFIG_TMPFS_POSIX_ACL
  2264. sb->s_flags |= MS_POSIXACL;
  2265. #endif
  2266. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
  2267. if (!inode)
  2268. goto failed;
  2269. inode->i_uid = sbinfo->uid;
  2270. inode->i_gid = sbinfo->gid;
  2271. sb->s_root = d_make_root(inode);
  2272. if (!sb->s_root)
  2273. goto failed;
  2274. return 0;
  2275. failed:
  2276. shmem_put_super(sb);
  2277. return err;
  2278. }
  2279. static struct kmem_cache *shmem_inode_cachep;
  2280. static struct inode *shmem_alloc_inode(struct super_block *sb)
  2281. {
  2282. struct shmem_inode_info *info;
  2283. info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  2284. if (!info)
  2285. return NULL;
  2286. return &info->vfs_inode;
  2287. }
  2288. static void shmem_destroy_callback(struct rcu_head *head)
  2289. {
  2290. struct inode *inode = container_of(head, struct inode, i_rcu);
  2291. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  2292. }
  2293. static void shmem_destroy_inode(struct inode *inode)
  2294. {
  2295. if (S_ISREG(inode->i_mode))
  2296. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  2297. call_rcu(&inode->i_rcu, shmem_destroy_callback);
  2298. }
  2299. static void shmem_init_inode(void *foo)
  2300. {
  2301. struct shmem_inode_info *info = foo;
  2302. inode_init_once(&info->vfs_inode);
  2303. }
  2304. static int shmem_init_inodecache(void)
  2305. {
  2306. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  2307. sizeof(struct shmem_inode_info),
  2308. 0, SLAB_PANIC, shmem_init_inode);
  2309. return 0;
  2310. }
  2311. static void shmem_destroy_inodecache(void)
  2312. {
  2313. kmem_cache_destroy(shmem_inode_cachep);
  2314. }
  2315. static const struct address_space_operations shmem_aops = {
  2316. .writepage = shmem_writepage,
  2317. .set_page_dirty = __set_page_dirty_no_writeback,
  2318. #ifdef CONFIG_TMPFS
  2319. .write_begin = shmem_write_begin,
  2320. .write_end = shmem_write_end,
  2321. #endif
  2322. .migratepage = migrate_page,
  2323. .error_remove_page = generic_error_remove_page,
  2324. };
  2325. static const struct file_operations shmem_file_operations = {
  2326. .mmap = shmem_mmap,
  2327. #ifdef CONFIG_TMPFS
  2328. .llseek = shmem_file_llseek,
  2329. .read = do_sync_read,
  2330. .write = do_sync_write,
  2331. .aio_read = shmem_file_aio_read,
  2332. .aio_write = generic_file_aio_write,
  2333. .fsync = noop_fsync,
  2334. .splice_read = shmem_file_splice_read,
  2335. .splice_write = generic_file_splice_write,
  2336. .fallocate = shmem_fallocate,
  2337. #endif
  2338. };
  2339. static const struct inode_operations shmem_inode_operations = {
  2340. .setattr = shmem_setattr,
  2341. #ifdef CONFIG_TMPFS_XATTR
  2342. .setxattr = shmem_setxattr,
  2343. .getxattr = shmem_getxattr,
  2344. .listxattr = shmem_listxattr,
  2345. .removexattr = shmem_removexattr,
  2346. .set_acl = simple_set_acl,
  2347. #endif
  2348. };
  2349. static const struct inode_operations shmem_dir_inode_operations = {
  2350. #ifdef CONFIG_TMPFS
  2351. .create = shmem_create,
  2352. .lookup = simple_lookup,
  2353. .link = shmem_link,
  2354. .unlink = shmem_unlink,
  2355. .symlink = shmem_symlink,
  2356. .mkdir = shmem_mkdir,
  2357. .rmdir = shmem_rmdir,
  2358. .mknod = shmem_mknod,
  2359. .rename = shmem_rename,
  2360. .tmpfile = shmem_tmpfile,
  2361. #endif
  2362. #ifdef CONFIG_TMPFS_XATTR
  2363. .setxattr = shmem_setxattr,
  2364. .getxattr = shmem_getxattr,
  2365. .listxattr = shmem_listxattr,
  2366. .removexattr = shmem_removexattr,
  2367. #endif
  2368. #ifdef CONFIG_TMPFS_POSIX_ACL
  2369. .setattr = shmem_setattr,
  2370. .set_acl = simple_set_acl,
  2371. #endif
  2372. };
  2373. static const struct inode_operations shmem_special_inode_operations = {
  2374. #ifdef CONFIG_TMPFS_XATTR
  2375. .setxattr = shmem_setxattr,
  2376. .getxattr = shmem_getxattr,
  2377. .listxattr = shmem_listxattr,
  2378. .removexattr = shmem_removexattr,
  2379. #endif
  2380. #ifdef CONFIG_TMPFS_POSIX_ACL
  2381. .setattr = shmem_setattr,
  2382. .set_acl = simple_set_acl,
  2383. #endif
  2384. };
  2385. static const struct super_operations shmem_ops = {
  2386. .alloc_inode = shmem_alloc_inode,
  2387. .destroy_inode = shmem_destroy_inode,
  2388. #ifdef CONFIG_TMPFS
  2389. .statfs = shmem_statfs,
  2390. .remount_fs = shmem_remount_fs,
  2391. .show_options = shmem_show_options,
  2392. #endif
  2393. .evict_inode = shmem_evict_inode,
  2394. .drop_inode = generic_delete_inode,
  2395. .put_super = shmem_put_super,
  2396. };
  2397. static const struct vm_operations_struct shmem_vm_ops = {
  2398. .fault = shmem_fault,
  2399. .map_pages = filemap_map_pages,
  2400. #ifdef CONFIG_NUMA
  2401. .set_policy = shmem_set_policy,
  2402. .get_policy = shmem_get_policy,
  2403. #endif
  2404. .remap_pages = generic_file_remap_pages,
  2405. };
  2406. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  2407. int flags, const char *dev_name, void *data)
  2408. {
  2409. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  2410. }
  2411. static struct file_system_type shmem_fs_type = {
  2412. .owner = THIS_MODULE,
  2413. .name = "tmpfs",
  2414. .mount = shmem_mount,
  2415. .kill_sb = kill_litter_super,
  2416. .fs_flags = FS_USERNS_MOUNT,
  2417. };
  2418. int __init shmem_init(void)
  2419. {
  2420. int error;
  2421. /* If rootfs called this, don't re-init */
  2422. if (shmem_inode_cachep)
  2423. return 0;
  2424. error = bdi_init(&shmem_backing_dev_info);
  2425. if (error)
  2426. goto out4;
  2427. error = shmem_init_inodecache();
  2428. if (error)
  2429. goto out3;
  2430. error = register_filesystem(&shmem_fs_type);
  2431. if (error) {
  2432. printk(KERN_ERR "Could not register tmpfs\n");
  2433. goto out2;
  2434. }
  2435. shm_mnt = kern_mount(&shmem_fs_type);
  2436. if (IS_ERR(shm_mnt)) {
  2437. error = PTR_ERR(shm_mnt);
  2438. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2439. goto out1;
  2440. }
  2441. return 0;
  2442. out1:
  2443. unregister_filesystem(&shmem_fs_type);
  2444. out2:
  2445. shmem_destroy_inodecache();
  2446. out3:
  2447. bdi_destroy(&shmem_backing_dev_info);
  2448. out4:
  2449. shm_mnt = ERR_PTR(error);
  2450. return error;
  2451. }
  2452. #else /* !CONFIG_SHMEM */
  2453. /*
  2454. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  2455. *
  2456. * This is intended for small system where the benefits of the full
  2457. * shmem code (swap-backed and resource-limited) are outweighed by
  2458. * their complexity. On systems without swap this code should be
  2459. * effectively equivalent, but much lighter weight.
  2460. */
  2461. static struct file_system_type shmem_fs_type = {
  2462. .name = "tmpfs",
  2463. .mount = ramfs_mount,
  2464. .kill_sb = kill_litter_super,
  2465. .fs_flags = FS_USERNS_MOUNT,
  2466. };
  2467. int __init shmem_init(void)
  2468. {
  2469. BUG_ON(register_filesystem(&shmem_fs_type) != 0);
  2470. shm_mnt = kern_mount(&shmem_fs_type);
  2471. BUG_ON(IS_ERR(shm_mnt));
  2472. return 0;
  2473. }
  2474. int shmem_unuse(swp_entry_t swap, struct page *page)
  2475. {
  2476. return 0;
  2477. }
  2478. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  2479. {
  2480. return 0;
  2481. }
  2482. void shmem_unlock_mapping(struct address_space *mapping)
  2483. {
  2484. }
  2485. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  2486. {
  2487. truncate_inode_pages_range(inode->i_mapping, lstart, lend);
  2488. }
  2489. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  2490. #define shmem_vm_ops generic_file_vm_ops
  2491. #define shmem_file_operations ramfs_file_operations
  2492. #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
  2493. #define shmem_acct_size(flags, size) 0
  2494. #define shmem_unacct_size(flags, size) do {} while (0)
  2495. #endif /* CONFIG_SHMEM */
  2496. /* common code */
  2497. static struct dentry_operations anon_ops = {
  2498. .d_dname = simple_dname
  2499. };
  2500. static struct file *__shmem_file_setup(const char *name, loff_t size,
  2501. unsigned long flags, unsigned int i_flags)
  2502. {
  2503. struct file *res;
  2504. struct inode *inode;
  2505. struct path path;
  2506. struct super_block *sb;
  2507. struct qstr this;
  2508. if (IS_ERR(shm_mnt))
  2509. return ERR_CAST(shm_mnt);
  2510. if (size < 0 || size > MAX_LFS_FILESIZE)
  2511. return ERR_PTR(-EINVAL);
  2512. if (shmem_acct_size(flags, size))
  2513. return ERR_PTR(-ENOMEM);
  2514. res = ERR_PTR(-ENOMEM);
  2515. this.name = name;
  2516. this.len = strlen(name);
  2517. this.hash = 0; /* will go */
  2518. sb = shm_mnt->mnt_sb;
  2519. path.dentry = d_alloc_pseudo(sb, &this);
  2520. if (!path.dentry)
  2521. goto put_memory;
  2522. d_set_d_op(path.dentry, &anon_ops);
  2523. path.mnt = mntget(shm_mnt);
  2524. res = ERR_PTR(-ENOSPC);
  2525. inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
  2526. if (!inode)
  2527. goto put_dentry;
  2528. inode->i_flags |= i_flags;
  2529. d_instantiate(path.dentry, inode);
  2530. inode->i_size = size;
  2531. clear_nlink(inode); /* It is unlinked */
  2532. res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
  2533. if (IS_ERR(res))
  2534. goto put_dentry;
  2535. res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  2536. &shmem_file_operations);
  2537. if (IS_ERR(res))
  2538. goto put_dentry;
  2539. return res;
  2540. put_dentry:
  2541. path_put(&path);
  2542. put_memory:
  2543. shmem_unacct_size(flags, size);
  2544. return res;
  2545. }
  2546. /**
  2547. * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
  2548. * kernel internal. There will be NO LSM permission checks against the
  2549. * underlying inode. So users of this interface must do LSM checks at a
  2550. * higher layer. The one user is the big_key implementation. LSM checks
  2551. * are provided at the key level rather than the inode level.
  2552. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2553. * @size: size to be set for the file
  2554. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2555. */
  2556. struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
  2557. {
  2558. return __shmem_file_setup(name, size, flags, S_PRIVATE);
  2559. }
  2560. /**
  2561. * shmem_file_setup - get an unlinked file living in tmpfs
  2562. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2563. * @size: size to be set for the file
  2564. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2565. */
  2566. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  2567. {
  2568. return __shmem_file_setup(name, size, flags, 0);
  2569. }
  2570. EXPORT_SYMBOL_GPL(shmem_file_setup);
  2571. /**
  2572. * shmem_zero_setup - setup a shared anonymous mapping
  2573. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2574. */
  2575. int shmem_zero_setup(struct vm_area_struct *vma)
  2576. {
  2577. struct file *file;
  2578. loff_t size = vma->vm_end - vma->vm_start;
  2579. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2580. if (IS_ERR(file))
  2581. return PTR_ERR(file);
  2582. if (vma->vm_file)
  2583. fput(vma->vm_file);
  2584. vma->vm_file = file;
  2585. vma->vm_ops = &shmem_vm_ops;
  2586. return 0;
  2587. }
  2588. /**
  2589. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  2590. * @mapping: the page's address_space
  2591. * @index: the page index
  2592. * @gfp: the page allocator flags to use if allocating
  2593. *
  2594. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  2595. * with any new page allocations done using the specified allocation flags.
  2596. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  2597. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  2598. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  2599. *
  2600. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  2601. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  2602. */
  2603. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  2604. pgoff_t index, gfp_t gfp)
  2605. {
  2606. #ifdef CONFIG_SHMEM
  2607. struct inode *inode = mapping->host;
  2608. struct page *page;
  2609. int error;
  2610. BUG_ON(mapping->a_ops != &shmem_aops);
  2611. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
  2612. if (error)
  2613. page = ERR_PTR(error);
  2614. else
  2615. unlock_page(page);
  2616. return page;
  2617. #else
  2618. /*
  2619. * The tiny !SHMEM case uses ramfs without swap
  2620. */
  2621. return read_cache_page_gfp(mapping, index, gfp);
  2622. #endif
  2623. }
  2624. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);