shmem.c 77 KB

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