shmem.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638
  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-2005 Hugh Dickins.
  10. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  11. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  12. *
  13. * Extended attribute support for tmpfs:
  14. * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  15. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  16. *
  17. * This file is released under the GPL.
  18. */
  19. /*
  20. * This virtual memory filesystem is heavily based on the ramfs. It
  21. * extends ramfs by the ability to use swap and honor resource limits
  22. * which makes it a completely usable filesystem.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/fs.h>
  27. #include <linux/xattr.h>
  28. #include <linux/exportfs.h>
  29. #include <linux/generic_acl.h>
  30. #include <linux/mm.h>
  31. #include <linux/mman.h>
  32. #include <linux/file.h>
  33. #include <linux/swap.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/string.h>
  36. #include <linux/slab.h>
  37. #include <linux/backing-dev.h>
  38. #include <linux/shmem_fs.h>
  39. #include <linux/mount.h>
  40. #include <linux/writeback.h>
  41. #include <linux/vfs.h>
  42. #include <linux/blkdev.h>
  43. #include <linux/security.h>
  44. #include <linux/swapops.h>
  45. #include <linux/mempolicy.h>
  46. #include <linux/namei.h>
  47. #include <linux/ctype.h>
  48. #include <linux/migrate.h>
  49. #include <linux/highmem.h>
  50. #include <linux/seq_file.h>
  51. #include <asm/uaccess.h>
  52. #include <asm/div64.h>
  53. #include <asm/pgtable.h>
  54. /* This magic number is used in glibc for posix shared memory */
  55. #define TMPFS_MAGIC 0x01021994
  56. #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
  57. #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
  58. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  59. #define SHMEM_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
  60. #define SHMEM_MAX_BYTES ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
  61. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  62. /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
  63. #define SHMEM_PAGEIN VM_READ
  64. #define SHMEM_TRUNCATE VM_WRITE
  65. /* Definition to limit shmem_truncate's steps between cond_rescheds */
  66. #define LATENCY_LIMIT 64
  67. /* Pretend that each entry is of this size in directory's i_size */
  68. #define BOGO_DIRENT_SIZE 20
  69. /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
  70. enum sgp_type {
  71. SGP_READ, /* don't exceed i_size, don't allocate page */
  72. SGP_CACHE, /* don't exceed i_size, may allocate page */
  73. SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
  74. SGP_WRITE, /* may exceed i_size, may allocate page */
  75. };
  76. #ifdef CONFIG_TMPFS
  77. static unsigned long shmem_default_max_blocks(void)
  78. {
  79. return totalram_pages / 2;
  80. }
  81. static unsigned long shmem_default_max_inodes(void)
  82. {
  83. return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
  84. }
  85. #endif
  86. static int shmem_getpage(struct inode *inode, unsigned long idx,
  87. struct page **pagep, enum sgp_type sgp, int *type);
  88. static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
  89. {
  90. /*
  91. * The above definition of ENTRIES_PER_PAGE, and the use of
  92. * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
  93. * might be reconsidered if it ever diverges from PAGE_SIZE.
  94. *
  95. * Mobility flags are masked out as swap vectors cannot move
  96. */
  97. return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
  98. PAGE_CACHE_SHIFT-PAGE_SHIFT);
  99. }
  100. static inline void shmem_dir_free(struct page *page)
  101. {
  102. __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
  103. }
  104. static struct page **shmem_dir_map(struct page *page)
  105. {
  106. return (struct page **)kmap_atomic(page, KM_USER0);
  107. }
  108. static inline void shmem_dir_unmap(struct page **dir)
  109. {
  110. kunmap_atomic(dir, KM_USER0);
  111. }
  112. static swp_entry_t *shmem_swp_map(struct page *page)
  113. {
  114. return (swp_entry_t *)kmap_atomic(page, KM_USER1);
  115. }
  116. static inline void shmem_swp_balance_unmap(void)
  117. {
  118. /*
  119. * When passing a pointer to an i_direct entry, to code which
  120. * also handles indirect entries and so will shmem_swp_unmap,
  121. * we must arrange for the preempt count to remain in balance.
  122. * What kmap_atomic of a lowmem page does depends on config
  123. * and architecture, so pretend to kmap_atomic some lowmem page.
  124. */
  125. (void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
  126. }
  127. static inline void shmem_swp_unmap(swp_entry_t *entry)
  128. {
  129. kunmap_atomic(entry, KM_USER1);
  130. }
  131. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  132. {
  133. return sb->s_fs_info;
  134. }
  135. /*
  136. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  137. * for shared memory and for shared anonymous (/dev/zero) mappings
  138. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  139. * consistent with the pre-accounting of private mappings ...
  140. */
  141. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  142. {
  143. return (flags & VM_ACCOUNT)?
  144. security_vm_enough_memory(VM_ACCT(size)): 0;
  145. }
  146. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  147. {
  148. if (flags & VM_ACCOUNT)
  149. vm_unacct_memory(VM_ACCT(size));
  150. }
  151. /*
  152. * ... whereas tmpfs objects are accounted incrementally as
  153. * pages are allocated, in order to allow huge sparse files.
  154. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  155. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  156. */
  157. static inline int shmem_acct_block(unsigned long flags)
  158. {
  159. return (flags & VM_ACCOUNT)?
  160. 0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
  161. }
  162. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  163. {
  164. if (!(flags & VM_ACCOUNT))
  165. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  166. }
  167. static const struct super_operations shmem_ops;
  168. static const struct address_space_operations shmem_aops;
  169. static const struct file_operations shmem_file_operations;
  170. static const struct inode_operations shmem_inode_operations;
  171. static const struct inode_operations shmem_dir_inode_operations;
  172. static const struct inode_operations shmem_special_inode_operations;
  173. static struct vm_operations_struct shmem_vm_ops;
  174. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  175. .ra_pages = 0, /* No readahead */
  176. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  177. .unplug_io_fn = default_unplug_io_fn,
  178. };
  179. static LIST_HEAD(shmem_swaplist);
  180. static DEFINE_MUTEX(shmem_swaplist_mutex);
  181. static void shmem_free_blocks(struct inode *inode, long pages)
  182. {
  183. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  184. if (sbinfo->max_blocks) {
  185. spin_lock(&sbinfo->stat_lock);
  186. sbinfo->free_blocks += pages;
  187. inode->i_blocks -= pages*BLOCKS_PER_PAGE;
  188. spin_unlock(&sbinfo->stat_lock);
  189. }
  190. }
  191. static int shmem_reserve_inode(struct super_block *sb)
  192. {
  193. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  194. if (sbinfo->max_inodes) {
  195. spin_lock(&sbinfo->stat_lock);
  196. if (!sbinfo->free_inodes) {
  197. spin_unlock(&sbinfo->stat_lock);
  198. return -ENOSPC;
  199. }
  200. sbinfo->free_inodes--;
  201. spin_unlock(&sbinfo->stat_lock);
  202. }
  203. return 0;
  204. }
  205. static void shmem_free_inode(struct super_block *sb)
  206. {
  207. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  208. if (sbinfo->max_inodes) {
  209. spin_lock(&sbinfo->stat_lock);
  210. sbinfo->free_inodes++;
  211. spin_unlock(&sbinfo->stat_lock);
  212. }
  213. }
  214. /**
  215. * shmem_recalc_inode - recalculate the size of an inode
  216. * @inode: inode to recalc
  217. *
  218. * We have to calculate the free blocks since the mm can drop
  219. * undirtied hole pages behind our back.
  220. *
  221. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  222. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  223. *
  224. * It has to be called with the spinlock held.
  225. */
  226. static void shmem_recalc_inode(struct inode *inode)
  227. {
  228. struct shmem_inode_info *info = SHMEM_I(inode);
  229. long freed;
  230. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  231. if (freed > 0) {
  232. info->alloced -= freed;
  233. shmem_unacct_blocks(info->flags, freed);
  234. shmem_free_blocks(inode, freed);
  235. }
  236. }
  237. /**
  238. * shmem_swp_entry - find the swap vector position in the info structure
  239. * @info: info structure for the inode
  240. * @index: index of the page to find
  241. * @page: optional page to add to the structure. Has to be preset to
  242. * all zeros
  243. *
  244. * If there is no space allocated yet it will return NULL when
  245. * page is NULL, else it will use the page for the needed block,
  246. * setting it to NULL on return to indicate that it has been used.
  247. *
  248. * The swap vector is organized the following way:
  249. *
  250. * There are SHMEM_NR_DIRECT entries directly stored in the
  251. * shmem_inode_info structure. So small files do not need an addional
  252. * allocation.
  253. *
  254. * For pages with index > SHMEM_NR_DIRECT there is the pointer
  255. * i_indirect which points to a page which holds in the first half
  256. * doubly indirect blocks, in the second half triple indirect blocks:
  257. *
  258. * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
  259. * following layout (for SHMEM_NR_DIRECT == 16):
  260. *
  261. * i_indirect -> dir --> 16-19
  262. * | +-> 20-23
  263. * |
  264. * +-->dir2 --> 24-27
  265. * | +-> 28-31
  266. * | +-> 32-35
  267. * | +-> 36-39
  268. * |
  269. * +-->dir3 --> 40-43
  270. * +-> 44-47
  271. * +-> 48-51
  272. * +-> 52-55
  273. */
  274. static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
  275. {
  276. unsigned long offset;
  277. struct page **dir;
  278. struct page *subdir;
  279. if (index < SHMEM_NR_DIRECT) {
  280. shmem_swp_balance_unmap();
  281. return info->i_direct+index;
  282. }
  283. if (!info->i_indirect) {
  284. if (page) {
  285. info->i_indirect = *page;
  286. *page = NULL;
  287. }
  288. return NULL; /* need another page */
  289. }
  290. index -= SHMEM_NR_DIRECT;
  291. offset = index % ENTRIES_PER_PAGE;
  292. index /= ENTRIES_PER_PAGE;
  293. dir = shmem_dir_map(info->i_indirect);
  294. if (index >= ENTRIES_PER_PAGE/2) {
  295. index -= ENTRIES_PER_PAGE/2;
  296. dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
  297. index %= ENTRIES_PER_PAGE;
  298. subdir = *dir;
  299. if (!subdir) {
  300. if (page) {
  301. *dir = *page;
  302. *page = NULL;
  303. }
  304. shmem_dir_unmap(dir);
  305. return NULL; /* need another page */
  306. }
  307. shmem_dir_unmap(dir);
  308. dir = shmem_dir_map(subdir);
  309. }
  310. dir += index;
  311. subdir = *dir;
  312. if (!subdir) {
  313. if (!page || !(subdir = *page)) {
  314. shmem_dir_unmap(dir);
  315. return NULL; /* need a page */
  316. }
  317. *dir = subdir;
  318. *page = NULL;
  319. }
  320. shmem_dir_unmap(dir);
  321. return shmem_swp_map(subdir) + offset;
  322. }
  323. static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
  324. {
  325. long incdec = value? 1: -1;
  326. entry->val = value;
  327. info->swapped += incdec;
  328. if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
  329. struct page *page = kmap_atomic_to_page(entry);
  330. set_page_private(page, page_private(page) + incdec);
  331. }
  332. }
  333. /**
  334. * shmem_swp_alloc - get the position of the swap entry for the page.
  335. * @info: info structure for the inode
  336. * @index: index of the page to find
  337. * @sgp: check and recheck i_size? skip allocation?
  338. *
  339. * If the entry does not exist, allocate it.
  340. */
  341. static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
  342. {
  343. struct inode *inode = &info->vfs_inode;
  344. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  345. struct page *page = NULL;
  346. swp_entry_t *entry;
  347. if (sgp != SGP_WRITE &&
  348. ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  349. return ERR_PTR(-EINVAL);
  350. while (!(entry = shmem_swp_entry(info, index, &page))) {
  351. if (sgp == SGP_READ)
  352. return shmem_swp_map(ZERO_PAGE(0));
  353. /*
  354. * Test free_blocks against 1 not 0, since we have 1 data
  355. * page (and perhaps indirect index pages) yet to allocate:
  356. * a waste to allocate index if we cannot allocate data.
  357. */
  358. if (sbinfo->max_blocks) {
  359. spin_lock(&sbinfo->stat_lock);
  360. if (sbinfo->free_blocks <= 1) {
  361. spin_unlock(&sbinfo->stat_lock);
  362. return ERR_PTR(-ENOSPC);
  363. }
  364. sbinfo->free_blocks--;
  365. inode->i_blocks += BLOCKS_PER_PAGE;
  366. spin_unlock(&sbinfo->stat_lock);
  367. }
  368. spin_unlock(&info->lock);
  369. page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping));
  370. if (page)
  371. set_page_private(page, 0);
  372. spin_lock(&info->lock);
  373. if (!page) {
  374. shmem_free_blocks(inode, 1);
  375. return ERR_PTR(-ENOMEM);
  376. }
  377. if (sgp != SGP_WRITE &&
  378. ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  379. entry = ERR_PTR(-EINVAL);
  380. break;
  381. }
  382. if (info->next_index <= index)
  383. info->next_index = index + 1;
  384. }
  385. if (page) {
  386. /* another task gave its page, or truncated the file */
  387. shmem_free_blocks(inode, 1);
  388. shmem_dir_free(page);
  389. }
  390. if (info->next_index <= index && !IS_ERR(entry))
  391. info->next_index = index + 1;
  392. return entry;
  393. }
  394. /**
  395. * shmem_free_swp - free some swap entries in a directory
  396. * @dir: pointer to the directory
  397. * @edir: pointer after last entry of the directory
  398. * @punch_lock: pointer to spinlock when needed for the holepunch case
  399. */
  400. static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
  401. spinlock_t *punch_lock)
  402. {
  403. spinlock_t *punch_unlock = NULL;
  404. swp_entry_t *ptr;
  405. int freed = 0;
  406. for (ptr = dir; ptr < edir; ptr++) {
  407. if (ptr->val) {
  408. if (unlikely(punch_lock)) {
  409. punch_unlock = punch_lock;
  410. punch_lock = NULL;
  411. spin_lock(punch_unlock);
  412. if (!ptr->val)
  413. continue;
  414. }
  415. free_swap_and_cache(*ptr);
  416. *ptr = (swp_entry_t){0};
  417. freed++;
  418. }
  419. }
  420. if (punch_unlock)
  421. spin_unlock(punch_unlock);
  422. return freed;
  423. }
  424. static int shmem_map_and_free_swp(struct page *subdir, int offset,
  425. int limit, struct page ***dir, spinlock_t *punch_lock)
  426. {
  427. swp_entry_t *ptr;
  428. int freed = 0;
  429. ptr = shmem_swp_map(subdir);
  430. for (; offset < limit; offset += LATENCY_LIMIT) {
  431. int size = limit - offset;
  432. if (size > LATENCY_LIMIT)
  433. size = LATENCY_LIMIT;
  434. freed += shmem_free_swp(ptr+offset, ptr+offset+size,
  435. punch_lock);
  436. if (need_resched()) {
  437. shmem_swp_unmap(ptr);
  438. if (*dir) {
  439. shmem_dir_unmap(*dir);
  440. *dir = NULL;
  441. }
  442. cond_resched();
  443. ptr = shmem_swp_map(subdir);
  444. }
  445. }
  446. shmem_swp_unmap(ptr);
  447. return freed;
  448. }
  449. static void shmem_free_pages(struct list_head *next)
  450. {
  451. struct page *page;
  452. int freed = 0;
  453. do {
  454. page = container_of(next, struct page, lru);
  455. next = next->next;
  456. shmem_dir_free(page);
  457. freed++;
  458. if (freed >= LATENCY_LIMIT) {
  459. cond_resched();
  460. freed = 0;
  461. }
  462. } while (next);
  463. }
  464. static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
  465. {
  466. struct shmem_inode_info *info = SHMEM_I(inode);
  467. unsigned long idx;
  468. unsigned long size;
  469. unsigned long limit;
  470. unsigned long stage;
  471. unsigned long diroff;
  472. struct page **dir;
  473. struct page *topdir;
  474. struct page *middir;
  475. struct page *subdir;
  476. swp_entry_t *ptr;
  477. LIST_HEAD(pages_to_free);
  478. long nr_pages_to_free = 0;
  479. long nr_swaps_freed = 0;
  480. int offset;
  481. int freed;
  482. int punch_hole;
  483. spinlock_t *needs_lock;
  484. spinlock_t *punch_lock;
  485. unsigned long upper_limit;
  486. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  487. idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  488. if (idx >= info->next_index)
  489. return;
  490. spin_lock(&info->lock);
  491. info->flags |= SHMEM_TRUNCATE;
  492. if (likely(end == (loff_t) -1)) {
  493. limit = info->next_index;
  494. upper_limit = SHMEM_MAX_INDEX;
  495. info->next_index = idx;
  496. needs_lock = NULL;
  497. punch_hole = 0;
  498. } else {
  499. if (end + 1 >= inode->i_size) { /* we may free a little more */
  500. limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
  501. PAGE_CACHE_SHIFT;
  502. upper_limit = SHMEM_MAX_INDEX;
  503. } else {
  504. limit = (end + 1) >> PAGE_CACHE_SHIFT;
  505. upper_limit = limit;
  506. }
  507. needs_lock = &info->lock;
  508. punch_hole = 1;
  509. }
  510. topdir = info->i_indirect;
  511. if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
  512. info->i_indirect = NULL;
  513. nr_pages_to_free++;
  514. list_add(&topdir->lru, &pages_to_free);
  515. }
  516. spin_unlock(&info->lock);
  517. if (info->swapped && idx < SHMEM_NR_DIRECT) {
  518. ptr = info->i_direct;
  519. size = limit;
  520. if (size > SHMEM_NR_DIRECT)
  521. size = SHMEM_NR_DIRECT;
  522. nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
  523. }
  524. /*
  525. * If there are no indirect blocks or we are punching a hole
  526. * below indirect blocks, nothing to be done.
  527. */
  528. if (!topdir || limit <= SHMEM_NR_DIRECT)
  529. goto done2;
  530. /*
  531. * The truncation case has already dropped info->lock, and we're safe
  532. * because i_size and next_index have already been lowered, preventing
  533. * access beyond. But in the punch_hole case, we still need to take
  534. * the lock when updating the swap directory, because there might be
  535. * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
  536. * shmem_writepage. However, whenever we find we can remove a whole
  537. * directory page (not at the misaligned start or end of the range),
  538. * we first NULLify its pointer in the level above, and then have no
  539. * need to take the lock when updating its contents: needs_lock and
  540. * punch_lock (either pointing to info->lock or NULL) manage this.
  541. */
  542. upper_limit -= SHMEM_NR_DIRECT;
  543. limit -= SHMEM_NR_DIRECT;
  544. idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
  545. offset = idx % ENTRIES_PER_PAGE;
  546. idx -= offset;
  547. dir = shmem_dir_map(topdir);
  548. stage = ENTRIES_PER_PAGEPAGE/2;
  549. if (idx < ENTRIES_PER_PAGEPAGE/2) {
  550. middir = topdir;
  551. diroff = idx/ENTRIES_PER_PAGE;
  552. } else {
  553. dir += ENTRIES_PER_PAGE/2;
  554. dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
  555. while (stage <= idx)
  556. stage += ENTRIES_PER_PAGEPAGE;
  557. middir = *dir;
  558. if (*dir) {
  559. diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
  560. ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
  561. if (!diroff && !offset && upper_limit >= stage) {
  562. if (needs_lock) {
  563. spin_lock(needs_lock);
  564. *dir = NULL;
  565. spin_unlock(needs_lock);
  566. needs_lock = NULL;
  567. } else
  568. *dir = NULL;
  569. nr_pages_to_free++;
  570. list_add(&middir->lru, &pages_to_free);
  571. }
  572. shmem_dir_unmap(dir);
  573. dir = shmem_dir_map(middir);
  574. } else {
  575. diroff = 0;
  576. offset = 0;
  577. idx = stage;
  578. }
  579. }
  580. for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
  581. if (unlikely(idx == stage)) {
  582. shmem_dir_unmap(dir);
  583. dir = shmem_dir_map(topdir) +
  584. ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
  585. while (!*dir) {
  586. dir++;
  587. idx += ENTRIES_PER_PAGEPAGE;
  588. if (idx >= limit)
  589. goto done1;
  590. }
  591. stage = idx + ENTRIES_PER_PAGEPAGE;
  592. middir = *dir;
  593. if (punch_hole)
  594. needs_lock = &info->lock;
  595. if (upper_limit >= stage) {
  596. if (needs_lock) {
  597. spin_lock(needs_lock);
  598. *dir = NULL;
  599. spin_unlock(needs_lock);
  600. needs_lock = NULL;
  601. } else
  602. *dir = NULL;
  603. nr_pages_to_free++;
  604. list_add(&middir->lru, &pages_to_free);
  605. }
  606. shmem_dir_unmap(dir);
  607. cond_resched();
  608. dir = shmem_dir_map(middir);
  609. diroff = 0;
  610. }
  611. punch_lock = needs_lock;
  612. subdir = dir[diroff];
  613. if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
  614. if (needs_lock) {
  615. spin_lock(needs_lock);
  616. dir[diroff] = NULL;
  617. spin_unlock(needs_lock);
  618. punch_lock = NULL;
  619. } else
  620. dir[diroff] = NULL;
  621. nr_pages_to_free++;
  622. list_add(&subdir->lru, &pages_to_free);
  623. }
  624. if (subdir && page_private(subdir) /* has swap entries */) {
  625. size = limit - idx;
  626. if (size > ENTRIES_PER_PAGE)
  627. size = ENTRIES_PER_PAGE;
  628. freed = shmem_map_and_free_swp(subdir,
  629. offset, size, &dir, punch_lock);
  630. if (!dir)
  631. dir = shmem_dir_map(middir);
  632. nr_swaps_freed += freed;
  633. if (offset || punch_lock) {
  634. spin_lock(&info->lock);
  635. set_page_private(subdir,
  636. page_private(subdir) - freed);
  637. spin_unlock(&info->lock);
  638. } else
  639. BUG_ON(page_private(subdir) != freed);
  640. }
  641. offset = 0;
  642. }
  643. done1:
  644. shmem_dir_unmap(dir);
  645. done2:
  646. if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
  647. /*
  648. * Call truncate_inode_pages again: racing shmem_unuse_inode
  649. * may have swizzled a page in from swap since vmtruncate or
  650. * generic_delete_inode did it, before we lowered next_index.
  651. * Also, though shmem_getpage checks i_size before adding to
  652. * cache, no recheck after: so fix the narrow window there too.
  653. *
  654. * Recalling truncate_inode_pages_range and unmap_mapping_range
  655. * every time for punch_hole (which never got a chance to clear
  656. * SHMEM_PAGEIN at the start of vmtruncate_range) is expensive,
  657. * yet hardly ever necessary: try to optimize them out later.
  658. */
  659. truncate_inode_pages_range(inode->i_mapping, start, end);
  660. if (punch_hole)
  661. unmap_mapping_range(inode->i_mapping, start,
  662. end - start, 1);
  663. }
  664. spin_lock(&info->lock);
  665. info->flags &= ~SHMEM_TRUNCATE;
  666. info->swapped -= nr_swaps_freed;
  667. if (nr_pages_to_free)
  668. shmem_free_blocks(inode, nr_pages_to_free);
  669. shmem_recalc_inode(inode);
  670. spin_unlock(&info->lock);
  671. /*
  672. * Empty swap vector directory pages to be freed?
  673. */
  674. if (!list_empty(&pages_to_free)) {
  675. pages_to_free.prev->next = NULL;
  676. shmem_free_pages(pages_to_free.next);
  677. }
  678. }
  679. static void shmem_truncate(struct inode *inode)
  680. {
  681. shmem_truncate_range(inode, inode->i_size, (loff_t)-1);
  682. }
  683. static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
  684. {
  685. struct inode *inode = dentry->d_inode;
  686. struct page *page = NULL;
  687. int error;
  688. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  689. if (attr->ia_size < inode->i_size) {
  690. /*
  691. * If truncating down to a partial page, then
  692. * if that page is already allocated, hold it
  693. * in memory until the truncation is over, so
  694. * truncate_partial_page cannnot miss it were
  695. * it assigned to swap.
  696. */
  697. if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
  698. (void) shmem_getpage(inode,
  699. attr->ia_size>>PAGE_CACHE_SHIFT,
  700. &page, SGP_READ, NULL);
  701. if (page)
  702. unlock_page(page);
  703. }
  704. /*
  705. * Reset SHMEM_PAGEIN flag so that shmem_truncate can
  706. * detect if any pages might have been added to cache
  707. * after truncate_inode_pages. But we needn't bother
  708. * if it's being fully truncated to zero-length: the
  709. * nrpages check is efficient enough in that case.
  710. */
  711. if (attr->ia_size) {
  712. struct shmem_inode_info *info = SHMEM_I(inode);
  713. spin_lock(&info->lock);
  714. info->flags &= ~SHMEM_PAGEIN;
  715. spin_unlock(&info->lock);
  716. }
  717. }
  718. }
  719. error = inode_change_ok(inode, attr);
  720. if (!error)
  721. error = inode_setattr(inode, attr);
  722. #ifdef CONFIG_TMPFS_POSIX_ACL
  723. if (!error && (attr->ia_valid & ATTR_MODE))
  724. error = generic_acl_chmod(inode, &shmem_acl_ops);
  725. #endif
  726. if (page)
  727. page_cache_release(page);
  728. return error;
  729. }
  730. static void shmem_delete_inode(struct inode *inode)
  731. {
  732. struct shmem_inode_info *info = SHMEM_I(inode);
  733. if (inode->i_op->truncate == shmem_truncate) {
  734. truncate_inode_pages(inode->i_mapping, 0);
  735. shmem_unacct_size(info->flags, inode->i_size);
  736. inode->i_size = 0;
  737. shmem_truncate(inode);
  738. if (!list_empty(&info->swaplist)) {
  739. mutex_lock(&shmem_swaplist_mutex);
  740. list_del_init(&info->swaplist);
  741. mutex_unlock(&shmem_swaplist_mutex);
  742. }
  743. }
  744. BUG_ON(inode->i_blocks);
  745. shmem_free_inode(inode->i_sb);
  746. clear_inode(inode);
  747. }
  748. static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
  749. {
  750. swp_entry_t *ptr;
  751. for (ptr = dir; ptr < edir; ptr++) {
  752. if (ptr->val == entry.val)
  753. return ptr - dir;
  754. }
  755. return -1;
  756. }
  757. static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
  758. {
  759. struct inode *inode;
  760. unsigned long idx;
  761. unsigned long size;
  762. unsigned long limit;
  763. unsigned long stage;
  764. struct page **dir;
  765. struct page *subdir;
  766. swp_entry_t *ptr;
  767. int offset;
  768. int error;
  769. idx = 0;
  770. ptr = info->i_direct;
  771. spin_lock(&info->lock);
  772. if (!info->swapped) {
  773. list_del_init(&info->swaplist);
  774. goto lost2;
  775. }
  776. limit = info->next_index;
  777. size = limit;
  778. if (size > SHMEM_NR_DIRECT)
  779. size = SHMEM_NR_DIRECT;
  780. offset = shmem_find_swp(entry, ptr, ptr+size);
  781. if (offset >= 0)
  782. goto found;
  783. if (!info->i_indirect)
  784. goto lost2;
  785. dir = shmem_dir_map(info->i_indirect);
  786. stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
  787. for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
  788. if (unlikely(idx == stage)) {
  789. shmem_dir_unmap(dir-1);
  790. if (cond_resched_lock(&info->lock)) {
  791. /* check it has not been truncated */
  792. if (limit > info->next_index) {
  793. limit = info->next_index;
  794. if (idx >= limit)
  795. goto lost2;
  796. }
  797. }
  798. dir = shmem_dir_map(info->i_indirect) +
  799. ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
  800. while (!*dir) {
  801. dir++;
  802. idx += ENTRIES_PER_PAGEPAGE;
  803. if (idx >= limit)
  804. goto lost1;
  805. }
  806. stage = idx + ENTRIES_PER_PAGEPAGE;
  807. subdir = *dir;
  808. shmem_dir_unmap(dir);
  809. dir = shmem_dir_map(subdir);
  810. }
  811. subdir = *dir;
  812. if (subdir && page_private(subdir)) {
  813. ptr = shmem_swp_map(subdir);
  814. size = limit - idx;
  815. if (size > ENTRIES_PER_PAGE)
  816. size = ENTRIES_PER_PAGE;
  817. offset = shmem_find_swp(entry, ptr, ptr+size);
  818. shmem_swp_unmap(ptr);
  819. if (offset >= 0) {
  820. shmem_dir_unmap(dir);
  821. goto found;
  822. }
  823. }
  824. }
  825. lost1:
  826. shmem_dir_unmap(dir-1);
  827. lost2:
  828. spin_unlock(&info->lock);
  829. return 0;
  830. found:
  831. idx += offset;
  832. inode = igrab(&info->vfs_inode);
  833. spin_unlock(&info->lock);
  834. /*
  835. * Move _head_ to start search for next from here.
  836. * But be careful: shmem_delete_inode checks list_empty without taking
  837. * mutex, and there's an instant in list_move_tail when info->swaplist
  838. * would appear empty, if it were the only one on shmem_swaplist. We
  839. * could avoid doing it if inode NULL; or use this minor optimization.
  840. */
  841. if (shmem_swaplist.next != &info->swaplist)
  842. list_move_tail(&shmem_swaplist, &info->swaplist);
  843. mutex_unlock(&shmem_swaplist_mutex);
  844. error = 1;
  845. if (!inode)
  846. goto out;
  847. /* Precharge page while we can wait, compensate afterwards */
  848. error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
  849. if (error)
  850. goto out;
  851. error = radix_tree_preload(GFP_KERNEL);
  852. if (error)
  853. goto uncharge;
  854. error = 1;
  855. spin_lock(&info->lock);
  856. ptr = shmem_swp_entry(info, idx, NULL);
  857. if (ptr && ptr->val == entry.val)
  858. error = add_to_page_cache(page, inode->i_mapping,
  859. idx, GFP_NOWAIT);
  860. if (error == -EEXIST) {
  861. struct page *filepage = find_get_page(inode->i_mapping, idx);
  862. error = 1;
  863. if (filepage) {
  864. /*
  865. * There might be a more uptodate page coming down
  866. * from a stacked writepage: forget our swappage if so.
  867. */
  868. if (PageUptodate(filepage))
  869. error = 0;
  870. page_cache_release(filepage);
  871. }
  872. }
  873. if (!error) {
  874. delete_from_swap_cache(page);
  875. set_page_dirty(page);
  876. info->flags |= SHMEM_PAGEIN;
  877. shmem_swp_set(info, ptr, 0);
  878. swap_free(entry);
  879. error = 1; /* not an error, but entry was found */
  880. }
  881. if (ptr)
  882. shmem_swp_unmap(ptr);
  883. spin_unlock(&info->lock);
  884. radix_tree_preload_end();
  885. uncharge:
  886. mem_cgroup_uncharge_page(page);
  887. out:
  888. unlock_page(page);
  889. page_cache_release(page);
  890. iput(inode); /* allows for NULL */
  891. return error;
  892. }
  893. /*
  894. * shmem_unuse() search for an eventually swapped out shmem page.
  895. */
  896. int shmem_unuse(swp_entry_t entry, struct page *page)
  897. {
  898. struct list_head *p, *next;
  899. struct shmem_inode_info *info;
  900. int found = 0;
  901. mutex_lock(&shmem_swaplist_mutex);
  902. list_for_each_safe(p, next, &shmem_swaplist) {
  903. info = list_entry(p, struct shmem_inode_info, swaplist);
  904. found = shmem_unuse_inode(info, entry, page);
  905. cond_resched();
  906. if (found)
  907. goto out;
  908. }
  909. mutex_unlock(&shmem_swaplist_mutex);
  910. out: return found; /* 0 or 1 or -ENOMEM */
  911. }
  912. /*
  913. * Move the page from the page cache to the swap cache.
  914. */
  915. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  916. {
  917. struct shmem_inode_info *info;
  918. swp_entry_t *entry, swap;
  919. struct address_space *mapping;
  920. unsigned long index;
  921. struct inode *inode;
  922. BUG_ON(!PageLocked(page));
  923. mapping = page->mapping;
  924. index = page->index;
  925. inode = mapping->host;
  926. info = SHMEM_I(inode);
  927. if (info->flags & VM_LOCKED)
  928. goto redirty;
  929. if (!total_swap_pages)
  930. goto redirty;
  931. /*
  932. * shmem_backing_dev_info's capabilities prevent regular writeback or
  933. * sync from ever calling shmem_writepage; but a stacking filesystem
  934. * may use the ->writepage of its underlying filesystem, in which case
  935. * tmpfs should write out to swap only in response to memory pressure,
  936. * and not for pdflush or sync. However, in those cases, we do still
  937. * want to check if there's a redundant swappage to be discarded.
  938. */
  939. if (wbc->for_reclaim)
  940. swap = get_swap_page();
  941. else
  942. swap.val = 0;
  943. spin_lock(&info->lock);
  944. if (index >= info->next_index) {
  945. BUG_ON(!(info->flags & SHMEM_TRUNCATE));
  946. goto unlock;
  947. }
  948. entry = shmem_swp_entry(info, index, NULL);
  949. if (entry->val) {
  950. /*
  951. * The more uptodate page coming down from a stacked
  952. * writepage should replace our old swappage.
  953. */
  954. free_swap_and_cache(*entry);
  955. shmem_swp_set(info, entry, 0);
  956. }
  957. shmem_recalc_inode(inode);
  958. if (swap.val && add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
  959. remove_from_page_cache(page);
  960. shmem_swp_set(info, entry, swap.val);
  961. shmem_swp_unmap(entry);
  962. if (list_empty(&info->swaplist))
  963. inode = igrab(inode);
  964. else
  965. inode = NULL;
  966. spin_unlock(&info->lock);
  967. swap_duplicate(swap);
  968. BUG_ON(page_mapped(page));
  969. page_cache_release(page); /* pagecache ref */
  970. set_page_dirty(page);
  971. unlock_page(page);
  972. if (inode) {
  973. mutex_lock(&shmem_swaplist_mutex);
  974. /* move instead of add in case we're racing */
  975. list_move_tail(&info->swaplist, &shmem_swaplist);
  976. mutex_unlock(&shmem_swaplist_mutex);
  977. iput(inode);
  978. }
  979. return 0;
  980. }
  981. shmem_swp_unmap(entry);
  982. unlock:
  983. spin_unlock(&info->lock);
  984. swap_free(swap);
  985. redirty:
  986. set_page_dirty(page);
  987. if (wbc->for_reclaim)
  988. return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
  989. unlock_page(page);
  990. return 0;
  991. }
  992. #ifdef CONFIG_NUMA
  993. #ifdef CONFIG_TMPFS
  994. static int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes)
  995. {
  996. char *nodelist = strchr(value, ':');
  997. int err = 1;
  998. if (nodelist) {
  999. /* NUL-terminate policy string */
  1000. *nodelist++ = '\0';
  1001. if (nodelist_parse(nodelist, *policy_nodes))
  1002. goto out;
  1003. if (!nodes_subset(*policy_nodes, node_states[N_HIGH_MEMORY]))
  1004. goto out;
  1005. }
  1006. if (!strcmp(value, "default")) {
  1007. *policy = MPOL_DEFAULT;
  1008. /* Don't allow a nodelist */
  1009. if (!nodelist)
  1010. err = 0;
  1011. } else if (!strcmp(value, "prefer")) {
  1012. *policy = MPOL_PREFERRED;
  1013. /* Insist on a nodelist of one node only */
  1014. if (nodelist) {
  1015. char *rest = nodelist;
  1016. while (isdigit(*rest))
  1017. rest++;
  1018. if (!*rest)
  1019. err = 0;
  1020. }
  1021. } else if (!strcmp(value, "bind")) {
  1022. *policy = MPOL_BIND;
  1023. /* Insist on a nodelist */
  1024. if (nodelist)
  1025. err = 0;
  1026. } else if (!strcmp(value, "interleave")) {
  1027. *policy = MPOL_INTERLEAVE;
  1028. /*
  1029. * Default to online nodes with memory if no nodelist
  1030. */
  1031. if (!nodelist)
  1032. *policy_nodes = node_states[N_HIGH_MEMORY];
  1033. err = 0;
  1034. }
  1035. out:
  1036. /* Restore string for error message */
  1037. if (nodelist)
  1038. *--nodelist = ':';
  1039. return err;
  1040. }
  1041. static void shmem_show_mpol(struct seq_file *seq, int policy,
  1042. const nodemask_t policy_nodes)
  1043. {
  1044. char *policy_string;
  1045. switch (policy) {
  1046. case MPOL_PREFERRED:
  1047. policy_string = "prefer";
  1048. break;
  1049. case MPOL_BIND:
  1050. policy_string = "bind";
  1051. break;
  1052. case MPOL_INTERLEAVE:
  1053. policy_string = "interleave";
  1054. break;
  1055. default:
  1056. /* MPOL_DEFAULT */
  1057. return;
  1058. }
  1059. seq_printf(seq, ",mpol=%s", policy_string);
  1060. if (policy != MPOL_INTERLEAVE ||
  1061. !nodes_equal(policy_nodes, node_states[N_HIGH_MEMORY])) {
  1062. char buffer[64];
  1063. int len;
  1064. len = nodelist_scnprintf(buffer, sizeof(buffer), policy_nodes);
  1065. if (len < sizeof(buffer))
  1066. seq_printf(seq, ":%s", buffer);
  1067. else
  1068. seq_printf(seq, ":?");
  1069. }
  1070. }
  1071. #endif /* CONFIG_TMPFS */
  1072. static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
  1073. struct shmem_inode_info *info, unsigned long idx)
  1074. {
  1075. struct vm_area_struct pvma;
  1076. struct page *page;
  1077. /* Create a pseudo vma that just contains the policy */
  1078. pvma.vm_start = 0;
  1079. pvma.vm_pgoff = idx;
  1080. pvma.vm_ops = NULL;
  1081. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
  1082. page = swapin_readahead(entry, gfp, &pvma, 0);
  1083. mpol_free(pvma.vm_policy);
  1084. return page;
  1085. }
  1086. static struct page *shmem_alloc_page(gfp_t gfp,
  1087. struct shmem_inode_info *info, unsigned long idx)
  1088. {
  1089. struct vm_area_struct pvma;
  1090. struct page *page;
  1091. /* Create a pseudo vma that just contains the policy */
  1092. pvma.vm_start = 0;
  1093. pvma.vm_pgoff = idx;
  1094. pvma.vm_ops = NULL;
  1095. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
  1096. page = alloc_page_vma(gfp, &pvma, 0);
  1097. mpol_free(pvma.vm_policy);
  1098. return page;
  1099. }
  1100. #else /* !CONFIG_NUMA */
  1101. #ifdef CONFIG_TMPFS
  1102. static inline int shmem_parse_mpol(char *value, int *policy,
  1103. nodemask_t *policy_nodes)
  1104. {
  1105. return 1;
  1106. }
  1107. static inline void shmem_show_mpol(struct seq_file *seq, int policy,
  1108. const nodemask_t policy_nodes)
  1109. {
  1110. }
  1111. #endif /* CONFIG_TMPFS */
  1112. static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
  1113. struct shmem_inode_info *info, unsigned long idx)
  1114. {
  1115. return swapin_readahead(entry, gfp, NULL, 0);
  1116. }
  1117. static inline struct page *shmem_alloc_page(gfp_t gfp,
  1118. struct shmem_inode_info *info, unsigned long idx)
  1119. {
  1120. return alloc_page(gfp);
  1121. }
  1122. #endif /* CONFIG_NUMA */
  1123. /*
  1124. * shmem_getpage - either get the page from swap or allocate a new one
  1125. *
  1126. * If we allocate a new one we do not mark it dirty. That's up to the
  1127. * vm. If we swap it in we mark it dirty since we also free the swap
  1128. * entry since a page cannot live in both the swap and page cache
  1129. */
  1130. static int shmem_getpage(struct inode *inode, unsigned long idx,
  1131. struct page **pagep, enum sgp_type sgp, int *type)
  1132. {
  1133. struct address_space *mapping = inode->i_mapping;
  1134. struct shmem_inode_info *info = SHMEM_I(inode);
  1135. struct shmem_sb_info *sbinfo;
  1136. struct page *filepage = *pagep;
  1137. struct page *swappage;
  1138. swp_entry_t *entry;
  1139. swp_entry_t swap;
  1140. gfp_t gfp;
  1141. int error;
  1142. if (idx >= SHMEM_MAX_INDEX)
  1143. return -EFBIG;
  1144. if (type)
  1145. *type = 0;
  1146. /*
  1147. * Normally, filepage is NULL on entry, and either found
  1148. * uptodate immediately, or allocated and zeroed, or read
  1149. * in under swappage, which is then assigned to filepage.
  1150. * But shmem_readpage (required for splice) passes in a locked
  1151. * filepage, which may be found not uptodate by other callers
  1152. * too, and may need to be copied from the swappage read in.
  1153. */
  1154. repeat:
  1155. if (!filepage)
  1156. filepage = find_lock_page(mapping, idx);
  1157. if (filepage && PageUptodate(filepage))
  1158. goto done;
  1159. error = 0;
  1160. gfp = mapping_gfp_mask(mapping);
  1161. if (!filepage) {
  1162. /*
  1163. * Try to preload while we can wait, to not make a habit of
  1164. * draining atomic reserves; but don't latch on to this cpu.
  1165. */
  1166. error = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
  1167. if (error)
  1168. goto failed;
  1169. radix_tree_preload_end();
  1170. }
  1171. spin_lock(&info->lock);
  1172. shmem_recalc_inode(inode);
  1173. entry = shmem_swp_alloc(info, idx, sgp);
  1174. if (IS_ERR(entry)) {
  1175. spin_unlock(&info->lock);
  1176. error = PTR_ERR(entry);
  1177. goto failed;
  1178. }
  1179. swap = *entry;
  1180. if (swap.val) {
  1181. /* Look it up and read it in.. */
  1182. swappage = lookup_swap_cache(swap);
  1183. if (!swappage) {
  1184. shmem_swp_unmap(entry);
  1185. /* here we actually do the io */
  1186. if (type && !(*type & VM_FAULT_MAJOR)) {
  1187. __count_vm_event(PGMAJFAULT);
  1188. *type |= VM_FAULT_MAJOR;
  1189. }
  1190. spin_unlock(&info->lock);
  1191. swappage = shmem_swapin(swap, gfp, info, idx);
  1192. if (!swappage) {
  1193. spin_lock(&info->lock);
  1194. entry = shmem_swp_alloc(info, idx, sgp);
  1195. if (IS_ERR(entry))
  1196. error = PTR_ERR(entry);
  1197. else {
  1198. if (entry->val == swap.val)
  1199. error = -ENOMEM;
  1200. shmem_swp_unmap(entry);
  1201. }
  1202. spin_unlock(&info->lock);
  1203. if (error)
  1204. goto failed;
  1205. goto repeat;
  1206. }
  1207. wait_on_page_locked(swappage);
  1208. page_cache_release(swappage);
  1209. goto repeat;
  1210. }
  1211. /* We have to do this with page locked to prevent races */
  1212. if (TestSetPageLocked(swappage)) {
  1213. shmem_swp_unmap(entry);
  1214. spin_unlock(&info->lock);
  1215. wait_on_page_locked(swappage);
  1216. page_cache_release(swappage);
  1217. goto repeat;
  1218. }
  1219. if (PageWriteback(swappage)) {
  1220. shmem_swp_unmap(entry);
  1221. spin_unlock(&info->lock);
  1222. wait_on_page_writeback(swappage);
  1223. unlock_page(swappage);
  1224. page_cache_release(swappage);
  1225. goto repeat;
  1226. }
  1227. if (!PageUptodate(swappage)) {
  1228. shmem_swp_unmap(entry);
  1229. spin_unlock(&info->lock);
  1230. unlock_page(swappage);
  1231. page_cache_release(swappage);
  1232. error = -EIO;
  1233. goto failed;
  1234. }
  1235. if (filepage) {
  1236. shmem_swp_set(info, entry, 0);
  1237. shmem_swp_unmap(entry);
  1238. delete_from_swap_cache(swappage);
  1239. spin_unlock(&info->lock);
  1240. copy_highpage(filepage, swappage);
  1241. unlock_page(swappage);
  1242. page_cache_release(swappage);
  1243. flush_dcache_page(filepage);
  1244. SetPageUptodate(filepage);
  1245. set_page_dirty(filepage);
  1246. swap_free(swap);
  1247. } else if (!(error = add_to_page_cache(
  1248. swappage, mapping, idx, GFP_NOWAIT))) {
  1249. info->flags |= SHMEM_PAGEIN;
  1250. shmem_swp_set(info, entry, 0);
  1251. shmem_swp_unmap(entry);
  1252. delete_from_swap_cache(swappage);
  1253. spin_unlock(&info->lock);
  1254. filepage = swappage;
  1255. set_page_dirty(filepage);
  1256. swap_free(swap);
  1257. } else {
  1258. shmem_swp_unmap(entry);
  1259. spin_unlock(&info->lock);
  1260. unlock_page(swappage);
  1261. if (error == -ENOMEM) {
  1262. /* allow reclaim from this memory cgroup */
  1263. error = mem_cgroup_cache_charge(swappage,
  1264. current->mm, gfp & ~__GFP_HIGHMEM);
  1265. if (error) {
  1266. page_cache_release(swappage);
  1267. goto failed;
  1268. }
  1269. mem_cgroup_uncharge_page(swappage);
  1270. }
  1271. page_cache_release(swappage);
  1272. goto repeat;
  1273. }
  1274. } else if (sgp == SGP_READ && !filepage) {
  1275. shmem_swp_unmap(entry);
  1276. filepage = find_get_page(mapping, idx);
  1277. if (filepage &&
  1278. (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
  1279. spin_unlock(&info->lock);
  1280. wait_on_page_locked(filepage);
  1281. page_cache_release(filepage);
  1282. filepage = NULL;
  1283. goto repeat;
  1284. }
  1285. spin_unlock(&info->lock);
  1286. } else {
  1287. shmem_swp_unmap(entry);
  1288. sbinfo = SHMEM_SB(inode->i_sb);
  1289. if (sbinfo->max_blocks) {
  1290. spin_lock(&sbinfo->stat_lock);
  1291. if (sbinfo->free_blocks == 0 ||
  1292. shmem_acct_block(info->flags)) {
  1293. spin_unlock(&sbinfo->stat_lock);
  1294. spin_unlock(&info->lock);
  1295. error = -ENOSPC;
  1296. goto failed;
  1297. }
  1298. sbinfo->free_blocks--;
  1299. inode->i_blocks += BLOCKS_PER_PAGE;
  1300. spin_unlock(&sbinfo->stat_lock);
  1301. } else if (shmem_acct_block(info->flags)) {
  1302. spin_unlock(&info->lock);
  1303. error = -ENOSPC;
  1304. goto failed;
  1305. }
  1306. if (!filepage) {
  1307. spin_unlock(&info->lock);
  1308. filepage = shmem_alloc_page(gfp, info, idx);
  1309. if (!filepage) {
  1310. shmem_unacct_blocks(info->flags, 1);
  1311. shmem_free_blocks(inode, 1);
  1312. error = -ENOMEM;
  1313. goto failed;
  1314. }
  1315. /* Precharge page while we can wait, compensate after */
  1316. error = mem_cgroup_cache_charge(filepage, current->mm,
  1317. gfp & ~__GFP_HIGHMEM);
  1318. if (error) {
  1319. page_cache_release(filepage);
  1320. shmem_unacct_blocks(info->flags, 1);
  1321. shmem_free_blocks(inode, 1);
  1322. filepage = NULL;
  1323. goto failed;
  1324. }
  1325. spin_lock(&info->lock);
  1326. entry = shmem_swp_alloc(info, idx, sgp);
  1327. if (IS_ERR(entry))
  1328. error = PTR_ERR(entry);
  1329. else {
  1330. swap = *entry;
  1331. shmem_swp_unmap(entry);
  1332. }
  1333. if (error || swap.val || 0 != add_to_page_cache_lru(
  1334. filepage, mapping, idx, GFP_NOWAIT)) {
  1335. spin_unlock(&info->lock);
  1336. mem_cgroup_uncharge_page(filepage);
  1337. page_cache_release(filepage);
  1338. shmem_unacct_blocks(info->flags, 1);
  1339. shmem_free_blocks(inode, 1);
  1340. filepage = NULL;
  1341. if (error)
  1342. goto failed;
  1343. goto repeat;
  1344. }
  1345. mem_cgroup_uncharge_page(filepage);
  1346. info->flags |= SHMEM_PAGEIN;
  1347. }
  1348. info->alloced++;
  1349. spin_unlock(&info->lock);
  1350. clear_highpage(filepage);
  1351. flush_dcache_page(filepage);
  1352. SetPageUptodate(filepage);
  1353. if (sgp == SGP_DIRTY)
  1354. set_page_dirty(filepage);
  1355. }
  1356. done:
  1357. *pagep = filepage;
  1358. return 0;
  1359. failed:
  1360. if (*pagep != filepage) {
  1361. unlock_page(filepage);
  1362. page_cache_release(filepage);
  1363. }
  1364. return error;
  1365. }
  1366. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1367. {
  1368. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1369. int error;
  1370. int ret;
  1371. if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  1372. return VM_FAULT_SIGBUS;
  1373. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
  1374. if (error)
  1375. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  1376. mark_page_accessed(vmf->page);
  1377. return ret | VM_FAULT_LOCKED;
  1378. }
  1379. #ifdef CONFIG_NUMA
  1380. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  1381. {
  1382. struct inode *i = vma->vm_file->f_path.dentry->d_inode;
  1383. return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
  1384. }
  1385. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  1386. unsigned long addr)
  1387. {
  1388. struct inode *i = vma->vm_file->f_path.dentry->d_inode;
  1389. unsigned long idx;
  1390. idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1391. return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
  1392. }
  1393. #endif
  1394. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1395. {
  1396. struct inode *inode = file->f_path.dentry->d_inode;
  1397. struct shmem_inode_info *info = SHMEM_I(inode);
  1398. int retval = -ENOMEM;
  1399. spin_lock(&info->lock);
  1400. if (lock && !(info->flags & VM_LOCKED)) {
  1401. if (!user_shm_lock(inode->i_size, user))
  1402. goto out_nomem;
  1403. info->flags |= VM_LOCKED;
  1404. }
  1405. if (!lock && (info->flags & VM_LOCKED) && user) {
  1406. user_shm_unlock(inode->i_size, user);
  1407. info->flags &= ~VM_LOCKED;
  1408. }
  1409. retval = 0;
  1410. out_nomem:
  1411. spin_unlock(&info->lock);
  1412. return retval;
  1413. }
  1414. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1415. {
  1416. file_accessed(file);
  1417. vma->vm_ops = &shmem_vm_ops;
  1418. vma->vm_flags |= VM_CAN_NONLINEAR;
  1419. return 0;
  1420. }
  1421. static struct inode *
  1422. shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
  1423. {
  1424. struct inode *inode;
  1425. struct shmem_inode_info *info;
  1426. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1427. if (shmem_reserve_inode(sb))
  1428. return NULL;
  1429. inode = new_inode(sb);
  1430. if (inode) {
  1431. inode->i_mode = mode;
  1432. inode->i_uid = current->fsuid;
  1433. inode->i_gid = current->fsgid;
  1434. inode->i_blocks = 0;
  1435. inode->i_mapping->a_ops = &shmem_aops;
  1436. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1437. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1438. inode->i_generation = get_seconds();
  1439. info = SHMEM_I(inode);
  1440. memset(info, 0, (char *)inode - (char *)info);
  1441. spin_lock_init(&info->lock);
  1442. INIT_LIST_HEAD(&info->swaplist);
  1443. switch (mode & S_IFMT) {
  1444. default:
  1445. inode->i_op = &shmem_special_inode_operations;
  1446. init_special_inode(inode, mode, dev);
  1447. break;
  1448. case S_IFREG:
  1449. inode->i_op = &shmem_inode_operations;
  1450. inode->i_fop = &shmem_file_operations;
  1451. mpol_shared_policy_init(&info->policy, sbinfo->policy,
  1452. &sbinfo->policy_nodes);
  1453. break;
  1454. case S_IFDIR:
  1455. inc_nlink(inode);
  1456. /* Some things misbehave if size == 0 on a directory */
  1457. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1458. inode->i_op = &shmem_dir_inode_operations;
  1459. inode->i_fop = &simple_dir_operations;
  1460. break;
  1461. case S_IFLNK:
  1462. /*
  1463. * Must not load anything in the rbtree,
  1464. * mpol_free_shared_policy will not be called.
  1465. */
  1466. mpol_shared_policy_init(&info->policy, MPOL_DEFAULT,
  1467. NULL);
  1468. break;
  1469. }
  1470. } else
  1471. shmem_free_inode(sb);
  1472. return inode;
  1473. }
  1474. #ifdef CONFIG_TMPFS
  1475. static const struct inode_operations shmem_symlink_inode_operations;
  1476. static const struct inode_operations shmem_symlink_inline_operations;
  1477. /*
  1478. * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
  1479. * but providing them allows a tmpfs file to be used for splice, sendfile, and
  1480. * below the loop driver, in the generic fashion that many filesystems support.
  1481. */
  1482. static int shmem_readpage(struct file *file, struct page *page)
  1483. {
  1484. struct inode *inode = page->mapping->host;
  1485. int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
  1486. unlock_page(page);
  1487. return error;
  1488. }
  1489. static int
  1490. shmem_write_begin(struct file *file, struct address_space *mapping,
  1491. loff_t pos, unsigned len, unsigned flags,
  1492. struct page **pagep, void **fsdata)
  1493. {
  1494. struct inode *inode = mapping->host;
  1495. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1496. *pagep = NULL;
  1497. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1498. }
  1499. static int
  1500. shmem_write_end(struct file *file, struct address_space *mapping,
  1501. loff_t pos, unsigned len, unsigned copied,
  1502. struct page *page, void *fsdata)
  1503. {
  1504. struct inode *inode = mapping->host;
  1505. if (pos + copied > inode->i_size)
  1506. i_size_write(inode, pos + copied);
  1507. unlock_page(page);
  1508. set_page_dirty(page);
  1509. page_cache_release(page);
  1510. return copied;
  1511. }
  1512. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1513. {
  1514. struct inode *inode = filp->f_path.dentry->d_inode;
  1515. struct address_space *mapping = inode->i_mapping;
  1516. unsigned long index, offset;
  1517. enum sgp_type sgp = SGP_READ;
  1518. /*
  1519. * Might this read be for a stacking filesystem? Then when reading
  1520. * holes of a sparse file, we actually need to allocate those pages,
  1521. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  1522. */
  1523. if (segment_eq(get_fs(), KERNEL_DS))
  1524. sgp = SGP_DIRTY;
  1525. index = *ppos >> PAGE_CACHE_SHIFT;
  1526. offset = *ppos & ~PAGE_CACHE_MASK;
  1527. for (;;) {
  1528. struct page *page = NULL;
  1529. unsigned long end_index, nr, ret;
  1530. loff_t i_size = i_size_read(inode);
  1531. end_index = i_size >> PAGE_CACHE_SHIFT;
  1532. if (index > end_index)
  1533. break;
  1534. if (index == end_index) {
  1535. nr = i_size & ~PAGE_CACHE_MASK;
  1536. if (nr <= offset)
  1537. break;
  1538. }
  1539. desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
  1540. if (desc->error) {
  1541. if (desc->error == -EINVAL)
  1542. desc->error = 0;
  1543. break;
  1544. }
  1545. if (page)
  1546. unlock_page(page);
  1547. /*
  1548. * We must evaluate after, since reads (unlike writes)
  1549. * are called without i_mutex protection against truncate
  1550. */
  1551. nr = PAGE_CACHE_SIZE;
  1552. i_size = i_size_read(inode);
  1553. end_index = i_size >> PAGE_CACHE_SHIFT;
  1554. if (index == end_index) {
  1555. nr = i_size & ~PAGE_CACHE_MASK;
  1556. if (nr <= offset) {
  1557. if (page)
  1558. page_cache_release(page);
  1559. break;
  1560. }
  1561. }
  1562. nr -= offset;
  1563. if (page) {
  1564. /*
  1565. * If users can be writing to this page using arbitrary
  1566. * virtual addresses, take care about potential aliasing
  1567. * before reading the page on the kernel side.
  1568. */
  1569. if (mapping_writably_mapped(mapping))
  1570. flush_dcache_page(page);
  1571. /*
  1572. * Mark the page accessed if we read the beginning.
  1573. */
  1574. if (!offset)
  1575. mark_page_accessed(page);
  1576. } else {
  1577. page = ZERO_PAGE(0);
  1578. page_cache_get(page);
  1579. }
  1580. /*
  1581. * Ok, we have the page, and it's up-to-date, so
  1582. * now we can copy it to user space...
  1583. *
  1584. * The actor routine returns how many bytes were actually used..
  1585. * NOTE! This may not be the same as how much of a user buffer
  1586. * we filled up (we may be padding etc), so we can only update
  1587. * "pos" here (the actor routine has to update the user buffer
  1588. * pointers and the remaining count).
  1589. */
  1590. ret = actor(desc, page, offset, nr);
  1591. offset += ret;
  1592. index += offset >> PAGE_CACHE_SHIFT;
  1593. offset &= ~PAGE_CACHE_MASK;
  1594. page_cache_release(page);
  1595. if (ret != nr || !desc->count)
  1596. break;
  1597. cond_resched();
  1598. }
  1599. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1600. file_accessed(filp);
  1601. }
  1602. static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  1603. {
  1604. read_descriptor_t desc;
  1605. if ((ssize_t) count < 0)
  1606. return -EINVAL;
  1607. if (!access_ok(VERIFY_WRITE, buf, count))
  1608. return -EFAULT;
  1609. if (!count)
  1610. return 0;
  1611. desc.written = 0;
  1612. desc.count = count;
  1613. desc.arg.buf = buf;
  1614. desc.error = 0;
  1615. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1616. if (desc.written)
  1617. return desc.written;
  1618. return desc.error;
  1619. }
  1620. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1621. {
  1622. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1623. buf->f_type = TMPFS_MAGIC;
  1624. buf->f_bsize = PAGE_CACHE_SIZE;
  1625. buf->f_namelen = NAME_MAX;
  1626. spin_lock(&sbinfo->stat_lock);
  1627. if (sbinfo->max_blocks) {
  1628. buf->f_blocks = sbinfo->max_blocks;
  1629. buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
  1630. }
  1631. if (sbinfo->max_inodes) {
  1632. buf->f_files = sbinfo->max_inodes;
  1633. buf->f_ffree = sbinfo->free_inodes;
  1634. }
  1635. /* else leave those fields 0 like simple_statfs */
  1636. spin_unlock(&sbinfo->stat_lock);
  1637. return 0;
  1638. }
  1639. /*
  1640. * File creation. Allocate an inode, and we're done..
  1641. */
  1642. static int
  1643. shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1644. {
  1645. struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
  1646. int error = -ENOSPC;
  1647. if (inode) {
  1648. error = security_inode_init_security(inode, dir, NULL, NULL,
  1649. NULL);
  1650. if (error) {
  1651. if (error != -EOPNOTSUPP) {
  1652. iput(inode);
  1653. return error;
  1654. }
  1655. }
  1656. error = shmem_acl_init(inode, dir);
  1657. if (error) {
  1658. iput(inode);
  1659. return error;
  1660. }
  1661. if (dir->i_mode & S_ISGID) {
  1662. inode->i_gid = dir->i_gid;
  1663. if (S_ISDIR(mode))
  1664. inode->i_mode |= S_ISGID;
  1665. }
  1666. dir->i_size += BOGO_DIRENT_SIZE;
  1667. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1668. d_instantiate(dentry, inode);
  1669. dget(dentry); /* Extra count - pin the dentry in core */
  1670. }
  1671. return error;
  1672. }
  1673. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1674. {
  1675. int error;
  1676. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1677. return error;
  1678. inc_nlink(dir);
  1679. return 0;
  1680. }
  1681. static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
  1682. struct nameidata *nd)
  1683. {
  1684. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1685. }
  1686. /*
  1687. * Link a file..
  1688. */
  1689. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1690. {
  1691. struct inode *inode = old_dentry->d_inode;
  1692. int ret;
  1693. /*
  1694. * No ordinary (disk based) filesystem counts links as inodes;
  1695. * but each new link needs a new dentry, pinning lowmem, and
  1696. * tmpfs dentries cannot be pruned until they are unlinked.
  1697. */
  1698. ret = shmem_reserve_inode(inode->i_sb);
  1699. if (ret)
  1700. goto out;
  1701. dir->i_size += BOGO_DIRENT_SIZE;
  1702. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1703. inc_nlink(inode);
  1704. atomic_inc(&inode->i_count); /* New dentry reference */
  1705. dget(dentry); /* Extra pinning count for the created dentry */
  1706. d_instantiate(dentry, inode);
  1707. out:
  1708. return ret;
  1709. }
  1710. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1711. {
  1712. struct inode *inode = dentry->d_inode;
  1713. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  1714. shmem_free_inode(inode->i_sb);
  1715. dir->i_size -= BOGO_DIRENT_SIZE;
  1716. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1717. drop_nlink(inode);
  1718. dput(dentry); /* Undo the count from "create" - this does all the work */
  1719. return 0;
  1720. }
  1721. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1722. {
  1723. if (!simple_empty(dentry))
  1724. return -ENOTEMPTY;
  1725. drop_nlink(dentry->d_inode);
  1726. drop_nlink(dir);
  1727. return shmem_unlink(dir, dentry);
  1728. }
  1729. /*
  1730. * The VFS layer already does all the dentry stuff for rename,
  1731. * we just have to decrement the usage count for the target if
  1732. * it exists so that the VFS layer correctly free's it when it
  1733. * gets overwritten.
  1734. */
  1735. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1736. {
  1737. struct inode *inode = old_dentry->d_inode;
  1738. int they_are_dirs = S_ISDIR(inode->i_mode);
  1739. if (!simple_empty(new_dentry))
  1740. return -ENOTEMPTY;
  1741. if (new_dentry->d_inode) {
  1742. (void) shmem_unlink(new_dir, new_dentry);
  1743. if (they_are_dirs)
  1744. drop_nlink(old_dir);
  1745. } else if (they_are_dirs) {
  1746. drop_nlink(old_dir);
  1747. inc_nlink(new_dir);
  1748. }
  1749. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1750. new_dir->i_size += BOGO_DIRENT_SIZE;
  1751. old_dir->i_ctime = old_dir->i_mtime =
  1752. new_dir->i_ctime = new_dir->i_mtime =
  1753. inode->i_ctime = CURRENT_TIME;
  1754. return 0;
  1755. }
  1756. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1757. {
  1758. int error;
  1759. int len;
  1760. struct inode *inode;
  1761. struct page *page = NULL;
  1762. char *kaddr;
  1763. struct shmem_inode_info *info;
  1764. len = strlen(symname) + 1;
  1765. if (len > PAGE_CACHE_SIZE)
  1766. return -ENAMETOOLONG;
  1767. inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
  1768. if (!inode)
  1769. return -ENOSPC;
  1770. error = security_inode_init_security(inode, dir, NULL, NULL,
  1771. NULL);
  1772. if (error) {
  1773. if (error != -EOPNOTSUPP) {
  1774. iput(inode);
  1775. return error;
  1776. }
  1777. error = 0;
  1778. }
  1779. info = SHMEM_I(inode);
  1780. inode->i_size = len-1;
  1781. if (len <= (char *)inode - (char *)info) {
  1782. /* do it inline */
  1783. memcpy(info, symname, len);
  1784. inode->i_op = &shmem_symlink_inline_operations;
  1785. } else {
  1786. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1787. if (error) {
  1788. iput(inode);
  1789. return error;
  1790. }
  1791. unlock_page(page);
  1792. inode->i_op = &shmem_symlink_inode_operations;
  1793. kaddr = kmap_atomic(page, KM_USER0);
  1794. memcpy(kaddr, symname, len);
  1795. kunmap_atomic(kaddr, KM_USER0);
  1796. set_page_dirty(page);
  1797. page_cache_release(page);
  1798. }
  1799. if (dir->i_mode & S_ISGID)
  1800. inode->i_gid = dir->i_gid;
  1801. dir->i_size += BOGO_DIRENT_SIZE;
  1802. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1803. d_instantiate(dentry, inode);
  1804. dget(dentry);
  1805. return 0;
  1806. }
  1807. static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
  1808. {
  1809. nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
  1810. return NULL;
  1811. }
  1812. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1813. {
  1814. struct page *page = NULL;
  1815. int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1816. nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
  1817. if (page)
  1818. unlock_page(page);
  1819. return page;
  1820. }
  1821. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1822. {
  1823. if (!IS_ERR(nd_get_link(nd))) {
  1824. struct page *page = cookie;
  1825. kunmap(page);
  1826. mark_page_accessed(page);
  1827. page_cache_release(page);
  1828. }
  1829. }
  1830. static const struct inode_operations shmem_symlink_inline_operations = {
  1831. .readlink = generic_readlink,
  1832. .follow_link = shmem_follow_link_inline,
  1833. };
  1834. static const struct inode_operations shmem_symlink_inode_operations = {
  1835. .truncate = shmem_truncate,
  1836. .readlink = generic_readlink,
  1837. .follow_link = shmem_follow_link,
  1838. .put_link = shmem_put_link,
  1839. };
  1840. #ifdef CONFIG_TMPFS_POSIX_ACL
  1841. /*
  1842. * Superblocks without xattr inode operations will get security.* xattr
  1843. * support from the VFS "for free". As soon as we have any other xattrs
  1844. * like ACLs, we also need to implement the security.* handlers at
  1845. * filesystem level, though.
  1846. */
  1847. static size_t shmem_xattr_security_list(struct inode *inode, char *list,
  1848. size_t list_len, const char *name,
  1849. size_t name_len)
  1850. {
  1851. return security_inode_listsecurity(inode, list, list_len);
  1852. }
  1853. static int shmem_xattr_security_get(struct inode *inode, const char *name,
  1854. void *buffer, size_t size)
  1855. {
  1856. if (strcmp(name, "") == 0)
  1857. return -EINVAL;
  1858. return xattr_getsecurity(inode, name, buffer, size);
  1859. }
  1860. static int shmem_xattr_security_set(struct inode *inode, const char *name,
  1861. const void *value, size_t size, int flags)
  1862. {
  1863. if (strcmp(name, "") == 0)
  1864. return -EINVAL;
  1865. return security_inode_setsecurity(inode, name, value, size, flags);
  1866. }
  1867. static struct xattr_handler shmem_xattr_security_handler = {
  1868. .prefix = XATTR_SECURITY_PREFIX,
  1869. .list = shmem_xattr_security_list,
  1870. .get = shmem_xattr_security_get,
  1871. .set = shmem_xattr_security_set,
  1872. };
  1873. static struct xattr_handler *shmem_xattr_handlers[] = {
  1874. &shmem_xattr_acl_access_handler,
  1875. &shmem_xattr_acl_default_handler,
  1876. &shmem_xattr_security_handler,
  1877. NULL
  1878. };
  1879. #endif
  1880. static struct dentry *shmem_get_parent(struct dentry *child)
  1881. {
  1882. return ERR_PTR(-ESTALE);
  1883. }
  1884. static int shmem_match(struct inode *ino, void *vfh)
  1885. {
  1886. __u32 *fh = vfh;
  1887. __u64 inum = fh[2];
  1888. inum = (inum << 32) | fh[1];
  1889. return ino->i_ino == inum && fh[0] == ino->i_generation;
  1890. }
  1891. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  1892. struct fid *fid, int fh_len, int fh_type)
  1893. {
  1894. struct inode *inode;
  1895. struct dentry *dentry = NULL;
  1896. u64 inum = fid->raw[2];
  1897. inum = (inum << 32) | fid->raw[1];
  1898. if (fh_len < 3)
  1899. return NULL;
  1900. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  1901. shmem_match, fid->raw);
  1902. if (inode) {
  1903. dentry = d_find_alias(inode);
  1904. iput(inode);
  1905. }
  1906. return dentry;
  1907. }
  1908. static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
  1909. int connectable)
  1910. {
  1911. struct inode *inode = dentry->d_inode;
  1912. if (*len < 3)
  1913. return 255;
  1914. if (hlist_unhashed(&inode->i_hash)) {
  1915. /* Unfortunately insert_inode_hash is not idempotent,
  1916. * so as we hash inodes here rather than at creation
  1917. * time, we need a lock to ensure we only try
  1918. * to do it once
  1919. */
  1920. static DEFINE_SPINLOCK(lock);
  1921. spin_lock(&lock);
  1922. if (hlist_unhashed(&inode->i_hash))
  1923. __insert_inode_hash(inode,
  1924. inode->i_ino + inode->i_generation);
  1925. spin_unlock(&lock);
  1926. }
  1927. fh[0] = inode->i_generation;
  1928. fh[1] = inode->i_ino;
  1929. fh[2] = ((__u64)inode->i_ino) >> 32;
  1930. *len = 3;
  1931. return 1;
  1932. }
  1933. static const struct export_operations shmem_export_ops = {
  1934. .get_parent = shmem_get_parent,
  1935. .encode_fh = shmem_encode_fh,
  1936. .fh_to_dentry = shmem_fh_to_dentry,
  1937. };
  1938. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  1939. bool remount)
  1940. {
  1941. char *this_char, *value, *rest;
  1942. while (options != NULL) {
  1943. this_char = options;
  1944. for (;;) {
  1945. /*
  1946. * NUL-terminate this option: unfortunately,
  1947. * mount options form a comma-separated list,
  1948. * but mpol's nodelist may also contain commas.
  1949. */
  1950. options = strchr(options, ',');
  1951. if (options == NULL)
  1952. break;
  1953. options++;
  1954. if (!isdigit(*options)) {
  1955. options[-1] = '\0';
  1956. break;
  1957. }
  1958. }
  1959. if (!*this_char)
  1960. continue;
  1961. if ((value = strchr(this_char,'=')) != NULL) {
  1962. *value++ = 0;
  1963. } else {
  1964. printk(KERN_ERR
  1965. "tmpfs: No value for mount option '%s'\n",
  1966. this_char);
  1967. return 1;
  1968. }
  1969. if (!strcmp(this_char,"size")) {
  1970. unsigned long long size;
  1971. size = memparse(value,&rest);
  1972. if (*rest == '%') {
  1973. size <<= PAGE_SHIFT;
  1974. size *= totalram_pages;
  1975. do_div(size, 100);
  1976. rest++;
  1977. }
  1978. if (*rest)
  1979. goto bad_val;
  1980. sbinfo->max_blocks =
  1981. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  1982. } else if (!strcmp(this_char,"nr_blocks")) {
  1983. sbinfo->max_blocks = memparse(value, &rest);
  1984. if (*rest)
  1985. goto bad_val;
  1986. } else if (!strcmp(this_char,"nr_inodes")) {
  1987. sbinfo->max_inodes = memparse(value, &rest);
  1988. if (*rest)
  1989. goto bad_val;
  1990. } else if (!strcmp(this_char,"mode")) {
  1991. if (remount)
  1992. continue;
  1993. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  1994. if (*rest)
  1995. goto bad_val;
  1996. } else if (!strcmp(this_char,"uid")) {
  1997. if (remount)
  1998. continue;
  1999. sbinfo->uid = simple_strtoul(value, &rest, 0);
  2000. if (*rest)
  2001. goto bad_val;
  2002. } else if (!strcmp(this_char,"gid")) {
  2003. if (remount)
  2004. continue;
  2005. sbinfo->gid = simple_strtoul(value, &rest, 0);
  2006. if (*rest)
  2007. goto bad_val;
  2008. } else if (!strcmp(this_char,"mpol")) {
  2009. if (shmem_parse_mpol(value, &sbinfo->policy,
  2010. &sbinfo->policy_nodes))
  2011. goto bad_val;
  2012. } else {
  2013. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  2014. this_char);
  2015. return 1;
  2016. }
  2017. }
  2018. return 0;
  2019. bad_val:
  2020. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  2021. value, this_char);
  2022. return 1;
  2023. }
  2024. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  2025. {
  2026. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2027. struct shmem_sb_info config = *sbinfo;
  2028. unsigned long blocks;
  2029. unsigned long inodes;
  2030. int error = -EINVAL;
  2031. if (shmem_parse_options(data, &config, true))
  2032. return error;
  2033. spin_lock(&sbinfo->stat_lock);
  2034. blocks = sbinfo->max_blocks - sbinfo->free_blocks;
  2035. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  2036. if (config.max_blocks < blocks)
  2037. goto out;
  2038. if (config.max_inodes < inodes)
  2039. goto out;
  2040. /*
  2041. * Those tests also disallow limited->unlimited while any are in
  2042. * use, so i_blocks will always be zero when max_blocks is zero;
  2043. * but we must separately disallow unlimited->limited, because
  2044. * in that case we have no record of how much is already in use.
  2045. */
  2046. if (config.max_blocks && !sbinfo->max_blocks)
  2047. goto out;
  2048. if (config.max_inodes && !sbinfo->max_inodes)
  2049. goto out;
  2050. error = 0;
  2051. sbinfo->max_blocks = config.max_blocks;
  2052. sbinfo->free_blocks = config.max_blocks - blocks;
  2053. sbinfo->max_inodes = config.max_inodes;
  2054. sbinfo->free_inodes = config.max_inodes - inodes;
  2055. sbinfo->policy = config.policy;
  2056. sbinfo->policy_nodes = config.policy_nodes;
  2057. out:
  2058. spin_unlock(&sbinfo->stat_lock);
  2059. return error;
  2060. }
  2061. static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
  2062. {
  2063. struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
  2064. if (sbinfo->max_blocks != shmem_default_max_blocks())
  2065. seq_printf(seq, ",size=%luk",
  2066. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  2067. if (sbinfo->max_inodes != shmem_default_max_inodes())
  2068. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  2069. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  2070. seq_printf(seq, ",mode=%03o", sbinfo->mode);
  2071. if (sbinfo->uid != 0)
  2072. seq_printf(seq, ",uid=%u", sbinfo->uid);
  2073. if (sbinfo->gid != 0)
  2074. seq_printf(seq, ",gid=%u", sbinfo->gid);
  2075. shmem_show_mpol(seq, sbinfo->policy, sbinfo->policy_nodes);
  2076. return 0;
  2077. }
  2078. #endif /* CONFIG_TMPFS */
  2079. static void shmem_put_super(struct super_block *sb)
  2080. {
  2081. kfree(sb->s_fs_info);
  2082. sb->s_fs_info = NULL;
  2083. }
  2084. static int shmem_fill_super(struct super_block *sb,
  2085. void *data, int silent)
  2086. {
  2087. struct inode *inode;
  2088. struct dentry *root;
  2089. struct shmem_sb_info *sbinfo;
  2090. int err = -ENOMEM;
  2091. /* Round up to L1_CACHE_BYTES to resist false sharing */
  2092. sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
  2093. L1_CACHE_BYTES), GFP_KERNEL);
  2094. if (!sbinfo)
  2095. return -ENOMEM;
  2096. sbinfo->max_blocks = 0;
  2097. sbinfo->max_inodes = 0;
  2098. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  2099. sbinfo->uid = current->fsuid;
  2100. sbinfo->gid = current->fsgid;
  2101. sbinfo->policy = MPOL_DEFAULT;
  2102. sbinfo->policy_nodes = node_states[N_HIGH_MEMORY];
  2103. sb->s_fs_info = sbinfo;
  2104. #ifdef CONFIG_TMPFS
  2105. /*
  2106. * Per default we only allow half of the physical ram per
  2107. * tmpfs instance, limiting inodes to one per page of lowmem;
  2108. * but the internal instance is left unlimited.
  2109. */
  2110. if (!(sb->s_flags & MS_NOUSER)) {
  2111. sbinfo->max_blocks = shmem_default_max_blocks();
  2112. sbinfo->max_inodes = shmem_default_max_inodes();
  2113. if (shmem_parse_options(data, sbinfo, false)) {
  2114. err = -EINVAL;
  2115. goto failed;
  2116. }
  2117. }
  2118. sb->s_export_op = &shmem_export_ops;
  2119. #else
  2120. sb->s_flags |= MS_NOUSER;
  2121. #endif
  2122. spin_lock_init(&sbinfo->stat_lock);
  2123. sbinfo->free_blocks = sbinfo->max_blocks;
  2124. sbinfo->free_inodes = sbinfo->max_inodes;
  2125. sb->s_maxbytes = SHMEM_MAX_BYTES;
  2126. sb->s_blocksize = PAGE_CACHE_SIZE;
  2127. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  2128. sb->s_magic = TMPFS_MAGIC;
  2129. sb->s_op = &shmem_ops;
  2130. sb->s_time_gran = 1;
  2131. #ifdef CONFIG_TMPFS_POSIX_ACL
  2132. sb->s_xattr = shmem_xattr_handlers;
  2133. sb->s_flags |= MS_POSIXACL;
  2134. #endif
  2135. inode = shmem_get_inode(sb, S_IFDIR | sbinfo->mode, 0);
  2136. if (!inode)
  2137. goto failed;
  2138. inode->i_uid = sbinfo->uid;
  2139. inode->i_gid = sbinfo->gid;
  2140. root = d_alloc_root(inode);
  2141. if (!root)
  2142. goto failed_iput;
  2143. sb->s_root = root;
  2144. return 0;
  2145. failed_iput:
  2146. iput(inode);
  2147. failed:
  2148. shmem_put_super(sb);
  2149. return err;
  2150. }
  2151. static struct kmem_cache *shmem_inode_cachep;
  2152. static struct inode *shmem_alloc_inode(struct super_block *sb)
  2153. {
  2154. struct shmem_inode_info *p;
  2155. p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  2156. if (!p)
  2157. return NULL;
  2158. return &p->vfs_inode;
  2159. }
  2160. static void shmem_destroy_inode(struct inode *inode)
  2161. {
  2162. if ((inode->i_mode & S_IFMT) == S_IFREG) {
  2163. /* only struct inode is valid if it's an inline symlink */
  2164. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  2165. }
  2166. shmem_acl_destroy_inode(inode);
  2167. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  2168. }
  2169. static void init_once(struct kmem_cache *cachep, void *foo)
  2170. {
  2171. struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
  2172. inode_init_once(&p->vfs_inode);
  2173. #ifdef CONFIG_TMPFS_POSIX_ACL
  2174. p->i_acl = NULL;
  2175. p->i_default_acl = NULL;
  2176. #endif
  2177. }
  2178. static int init_inodecache(void)
  2179. {
  2180. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  2181. sizeof(struct shmem_inode_info),
  2182. 0, SLAB_PANIC, init_once);
  2183. return 0;
  2184. }
  2185. static void destroy_inodecache(void)
  2186. {
  2187. kmem_cache_destroy(shmem_inode_cachep);
  2188. }
  2189. static const struct address_space_operations shmem_aops = {
  2190. .writepage = shmem_writepage,
  2191. .set_page_dirty = __set_page_dirty_no_writeback,
  2192. #ifdef CONFIG_TMPFS
  2193. .readpage = shmem_readpage,
  2194. .write_begin = shmem_write_begin,
  2195. .write_end = shmem_write_end,
  2196. #endif
  2197. .migratepage = migrate_page,
  2198. };
  2199. static const struct file_operations shmem_file_operations = {
  2200. .mmap = shmem_mmap,
  2201. #ifdef CONFIG_TMPFS
  2202. .llseek = generic_file_llseek,
  2203. .read = shmem_file_read,
  2204. .write = do_sync_write,
  2205. .aio_write = generic_file_aio_write,
  2206. .fsync = simple_sync_file,
  2207. .splice_read = generic_file_splice_read,
  2208. .splice_write = generic_file_splice_write,
  2209. #endif
  2210. };
  2211. static const struct inode_operations shmem_inode_operations = {
  2212. .truncate = shmem_truncate,
  2213. .setattr = shmem_notify_change,
  2214. .truncate_range = shmem_truncate_range,
  2215. #ifdef CONFIG_TMPFS_POSIX_ACL
  2216. .setxattr = generic_setxattr,
  2217. .getxattr = generic_getxattr,
  2218. .listxattr = generic_listxattr,
  2219. .removexattr = generic_removexattr,
  2220. .permission = shmem_permission,
  2221. #endif
  2222. };
  2223. static const struct inode_operations shmem_dir_inode_operations = {
  2224. #ifdef CONFIG_TMPFS
  2225. .create = shmem_create,
  2226. .lookup = simple_lookup,
  2227. .link = shmem_link,
  2228. .unlink = shmem_unlink,
  2229. .symlink = shmem_symlink,
  2230. .mkdir = shmem_mkdir,
  2231. .rmdir = shmem_rmdir,
  2232. .mknod = shmem_mknod,
  2233. .rename = shmem_rename,
  2234. #endif
  2235. #ifdef CONFIG_TMPFS_POSIX_ACL
  2236. .setattr = shmem_notify_change,
  2237. .setxattr = generic_setxattr,
  2238. .getxattr = generic_getxattr,
  2239. .listxattr = generic_listxattr,
  2240. .removexattr = generic_removexattr,
  2241. .permission = shmem_permission,
  2242. #endif
  2243. };
  2244. static const struct inode_operations shmem_special_inode_operations = {
  2245. #ifdef CONFIG_TMPFS_POSIX_ACL
  2246. .setattr = shmem_notify_change,
  2247. .setxattr = generic_setxattr,
  2248. .getxattr = generic_getxattr,
  2249. .listxattr = generic_listxattr,
  2250. .removexattr = generic_removexattr,
  2251. .permission = shmem_permission,
  2252. #endif
  2253. };
  2254. static const struct super_operations shmem_ops = {
  2255. .alloc_inode = shmem_alloc_inode,
  2256. .destroy_inode = shmem_destroy_inode,
  2257. #ifdef CONFIG_TMPFS
  2258. .statfs = shmem_statfs,
  2259. .remount_fs = shmem_remount_fs,
  2260. .show_options = shmem_show_options,
  2261. #endif
  2262. .delete_inode = shmem_delete_inode,
  2263. .drop_inode = generic_delete_inode,
  2264. .put_super = shmem_put_super,
  2265. };
  2266. static struct vm_operations_struct shmem_vm_ops = {
  2267. .fault = shmem_fault,
  2268. #ifdef CONFIG_NUMA
  2269. .set_policy = shmem_set_policy,
  2270. .get_policy = shmem_get_policy,
  2271. #endif
  2272. };
  2273. static int shmem_get_sb(struct file_system_type *fs_type,
  2274. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  2275. {
  2276. return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt);
  2277. }
  2278. static struct file_system_type tmpfs_fs_type = {
  2279. .owner = THIS_MODULE,
  2280. .name = "tmpfs",
  2281. .get_sb = shmem_get_sb,
  2282. .kill_sb = kill_litter_super,
  2283. };
  2284. static struct vfsmount *shm_mnt;
  2285. static int __init init_tmpfs(void)
  2286. {
  2287. int error;
  2288. error = bdi_init(&shmem_backing_dev_info);
  2289. if (error)
  2290. goto out4;
  2291. error = init_inodecache();
  2292. if (error)
  2293. goto out3;
  2294. error = register_filesystem(&tmpfs_fs_type);
  2295. if (error) {
  2296. printk(KERN_ERR "Could not register tmpfs\n");
  2297. goto out2;
  2298. }
  2299. shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
  2300. tmpfs_fs_type.name, NULL);
  2301. if (IS_ERR(shm_mnt)) {
  2302. error = PTR_ERR(shm_mnt);
  2303. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2304. goto out1;
  2305. }
  2306. return 0;
  2307. out1:
  2308. unregister_filesystem(&tmpfs_fs_type);
  2309. out2:
  2310. destroy_inodecache();
  2311. out3:
  2312. bdi_destroy(&shmem_backing_dev_info);
  2313. out4:
  2314. shm_mnt = ERR_PTR(error);
  2315. return error;
  2316. }
  2317. module_init(init_tmpfs)
  2318. /**
  2319. * shmem_file_setup - get an unlinked file living in tmpfs
  2320. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2321. * @size: size to be set for the file
  2322. * @flags: vm_flags
  2323. */
  2324. struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
  2325. {
  2326. int error;
  2327. struct file *file;
  2328. struct inode *inode;
  2329. struct dentry *dentry, *root;
  2330. struct qstr this;
  2331. if (IS_ERR(shm_mnt))
  2332. return (void *)shm_mnt;
  2333. if (size < 0 || size > SHMEM_MAX_BYTES)
  2334. return ERR_PTR(-EINVAL);
  2335. if (shmem_acct_size(flags, size))
  2336. return ERR_PTR(-ENOMEM);
  2337. error = -ENOMEM;
  2338. this.name = name;
  2339. this.len = strlen(name);
  2340. this.hash = 0; /* will go */
  2341. root = shm_mnt->mnt_root;
  2342. dentry = d_alloc(root, &this);
  2343. if (!dentry)
  2344. goto put_memory;
  2345. error = -ENFILE;
  2346. file = get_empty_filp();
  2347. if (!file)
  2348. goto put_dentry;
  2349. error = -ENOSPC;
  2350. inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
  2351. if (!inode)
  2352. goto close_file;
  2353. SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
  2354. d_instantiate(dentry, inode);
  2355. inode->i_size = size;
  2356. inode->i_nlink = 0; /* It is unlinked */
  2357. init_file(file, shm_mnt, dentry, FMODE_WRITE | FMODE_READ,
  2358. &shmem_file_operations);
  2359. return file;
  2360. close_file:
  2361. put_filp(file);
  2362. put_dentry:
  2363. dput(dentry);
  2364. put_memory:
  2365. shmem_unacct_size(flags, size);
  2366. return ERR_PTR(error);
  2367. }
  2368. /**
  2369. * shmem_zero_setup - setup a shared anonymous mapping
  2370. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2371. */
  2372. int shmem_zero_setup(struct vm_area_struct *vma)
  2373. {
  2374. struct file *file;
  2375. loff_t size = vma->vm_end - vma->vm_start;
  2376. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2377. if (IS_ERR(file))
  2378. return PTR_ERR(file);
  2379. if (vma->vm_file)
  2380. fput(vma->vm_file);
  2381. vma->vm_file = file;
  2382. vma->vm_ops = &shmem_vm_ops;
  2383. return 0;
  2384. }