2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
13 #include <afsconfig.h>
14 #include "afs/param.h"
18 #include "afs/sysincludes.h" /* Standard vendor system headers */
19 #include "afsincludes.h" /* Afs-based standard headers */
20 #include "afs/afs_stats.h" /* afs statistics */
21 #include "afs/afs_osi.h"
22 #include "afs/afs_md5.h"
24 /* Local variables. */
25 afs_rwlock_t afs_xcell; /* Export for cmdebug peeking at locks */
28 * AFSDB implementation:
30 * afs_StopAFSDB: terminate the AFSDB handler, used on shutdown
31 * afs_AFSDBHandler: entry point for user-space AFSDB request handler
32 * afs_GetCellHostsAFSDB: query the AFSDB handler and wait for response
33 * afs_LookupAFSDB: look up AFSDB for given cell name and create locally
36 afs_rwlock_t afsdb_client_lock; /* Serializes client requests */
37 afs_rwlock_t afsdb_req_lock; /* Serializes client requests */
38 static char afsdb_handler_running; /* Protected by GLOCK */
39 static char afsdb_handler_shutdown; /* Protected by GLOCK */
41 /* from cellconfig.h */
42 #define MAXCELLCHARS 64
44 /* lock moved to afsdb_req_lock for cmdebug */
51 * Terminate the AFSDB handler, used on shutdown.
56 if (afsdb_handler_running) {
57 afs_osi_Wakeup(&afsdb_req);
59 afsdb_handler_shutdown = 1;
60 afs_termState = AFSOP_STOP_RXEVENT;
61 afs_osi_Wakeup(&afs_termState);
66 * \brief Entry point for user-space AFSDB request handler.
67 * Reads cell data from kerlenMsg and add new cell, or alias.
68 * \param acellName Cell name. If a cell is found, it's name will be filled in here.
69 * \param acellNameLen Cell name length.
70 * \param kernelMsg Buffer containing data about host count, time out, and cell hosts ids.
71 * \return 0 for success, < 0 for error.
74 afs_AFSDBHandler(char *acellName, int acellNameLen, afs_int32 * kernelMsg)
76 afs_int32 timeout, code;
77 afs_int32 cellHosts[AFS_MAXCELLHOSTS];
79 if (afsdb_handler_shutdown)
81 afsdb_handler_running = 1;
83 ObtainSharedLock(&afsdb_req_lock, 683);
84 if (afsdb_req.pending) {
87 UpgradeSToWLock(&afsdb_req_lock, 684);
88 hostCount = kernelMsg[0];
89 timeout = kernelMsg[1];
91 timeout += osi_Time();
93 for (i = 0; i < AFS_MAXCELLHOSTS; i++) {
97 cellHosts[i] = kernelMsg[2 + i];
101 code = afs_NewCell(acellName, cellHosts, CNoSUID, NULL, 0, 0,
104 if (!hostCount || (code && code != EEXIST))
105 /* null out the cellname if the lookup failed */
106 afsdb_req.cellname = NULL;
108 /* If we found an alias, create it */
109 if (afs_strcasecmp(afsdb_req.cellname, acellName))
110 afs_NewCellAlias(afsdb_req.cellname, acellName);
112 /* Request completed, wake up the relevant thread */
113 afsdb_req.pending = 0;
114 afsdb_req.complete = 1;
115 afs_osi_Wakeup(&afsdb_req);
116 ConvertWToSLock(&afsdb_req_lock);
118 ConvertSToRLock(&afsdb_req_lock);
120 /* Wait for a request */
121 while (afsdb_req.pending == 0 && afs_termState != AFSOP_STOP_AFSDB) {
122 ReleaseReadLock(&afsdb_req_lock);
123 afs_osi_Sleep(&afsdb_req);
124 ObtainReadLock(&afsdb_req_lock);
127 /* Check if we're shutting down */
128 if (afs_termState == AFSOP_STOP_AFSDB) {
129 ReleaseReadLock(&afsdb_req_lock);
131 /* Inform anyone waiting for us that we're going away */
132 afsdb_handler_shutdown = 1;
133 afsdb_handler_running = 0;
134 afs_osi_Wakeup(&afsdb_req);
136 afs_termState = AFSOP_STOP_RXEVENT;
137 afs_osi_Wakeup(&afs_termState);
141 /* Return the lookup request to userspace */
142 strncpy(acellName, afsdb_req.cellname, acellNameLen);
143 ReleaseReadLock(&afsdb_req_lock);
148 * \brief Query the AFSDB handler and wait for response.
150 * \return 0 for success. < 0 is error.
153 afs_GetCellHostsAFSDB(char *acellName)
156 if (!afsdb_handler_running)
159 ObtainWriteLock(&afsdb_client_lock, 685);
160 ObtainWriteLock(&afsdb_req_lock, 686);
162 afsdb_req.cellname = acellName;
164 afsdb_req.complete = 0;
165 afsdb_req.pending = 1;
166 afs_osi_Wakeup(&afsdb_req);
167 ConvertWToRLock(&afsdb_req_lock);
169 while (afsdb_handler_running && !afsdb_req.complete) {
170 ReleaseReadLock(&afsdb_req_lock);
171 afs_osi_Sleep(&afsdb_req);
172 ObtainReadLock(&afsdb_req_lock);
175 ReleaseReadLock(&afsdb_req_lock);
176 ReleaseWriteLock(&afsdb_client_lock);
178 if (afsdb_req.cellname) {
186 * Look up AFSDB for given cell name and create locally.
187 * \param acellName Cell name.
190 afs_LookupAFSDB(char *acellName)
193 char *cellName = afs_strdup(acellName);
195 code = afs_GetCellHostsAFSDB(cellName);
196 afs_Trace2(afs_iclSetp, CM_TRACE_AFSDB, ICL_TYPE_STRING, cellName,
197 ICL_TYPE_INT32, code);
198 afs_osi_FreeStr(cellName);
202 * Cell name-to-ID mapping
204 * afs_cellname_new: create a new cell name, optional cell number
205 * afs_cellname_lookup_id: look up a cell name
206 * afs_cellname_lookup_name: look up a cell number
207 * afs_cellname_ref: note that this cell name was referenced somewhere
208 * afs_cellname_init: load the list of cells from given inode
209 * afs_cellname_write: write in-kernel list of cells to disk
212 struct cell_name *afs_cellname_head; /* Export for kdump */
213 static afs_dcache_id_t afs_cellname_inode;
214 static int afs_cellname_inode_set;
215 static int afs_cellname_dirty;
216 static afs_int32 afs_cellnum_next;
219 * Create a new cell name, optional cell number.
220 * \param name Name of cell.
221 * \param cellnum Cellname number.
222 * \return Initialized structure.
224 static struct cell_name *
225 afs_cellname_new(char *name, afs_int32 cellnum)
227 struct cell_name *cn;
230 cellnum = afs_cellnum_next;
232 cn = afs_osi_Alloc(sizeof(*cn));
233 osi_Assert(cn != NULL);
234 cn->next = afs_cellname_head;
235 cn->cellnum = cellnum;
236 cn->cellname = afs_strdup(name);
238 afs_cellname_head = cn;
240 if (cellnum >= afs_cellnum_next)
241 afs_cellnum_next = cellnum + 1;
247 * Look up a cell name by id.
251 static struct cell_name *
252 afs_cellname_lookup_id(afs_int32 cellnum)
254 struct cell_name *cn;
256 for (cn = afs_cellname_head; cn; cn = cn->next)
257 if (cn->cellnum == cellnum)
264 * Look up a cell name.
265 * \param name Cell name.
268 static struct cell_name *
269 afs_cellname_lookup_name(char *name)
271 struct cell_name *cn;
273 for (cn = afs_cellname_head; cn; cn = cn->next)
274 if (strcmp(cn->cellname, name) == 0)
281 * Note that this cell name was referenced somewhere.
285 afs_cellname_ref(struct cell_name *cn)
289 afs_cellname_dirty = 1;
294 * \brief Load the list of cells from given inode.
295 * \param inode Source inode.
297 * \return 0 for success. < 0 for error.
300 afs_cellname_init(afs_dcache_id_t *inode, int lookupcode)
302 struct osi_file *tfile;
305 ObtainWriteLock(&afs_xcell, 692);
307 afs_cellnum_next = 1;
308 afs_cellname_dirty = 0;
310 if (cacheDiskType == AFS_FCACHE_TYPE_MEM) {
311 ReleaseWriteLock(&afs_xcell);
315 ReleaseWriteLock(&afs_xcell);
319 tfile = osi_UFSOpen(inode);
321 ReleaseWriteLock(&afs_xcell);
325 afs_copy_inode(&afs_cellname_inode, inode);
326 afs_cellname_inode_set = 1;
329 afs_int32 cellnum, clen, magic;
330 struct cell_name *cn;
333 cc = afs_osi_Read(tfile, off, &magic, sizeof(magic));
334 if (cc != sizeof(magic))
336 if (magic != AFS_CELLINFO_MAGIC)
340 cc = afs_osi_Read(tfile, off, &cellnum, sizeof(cellnum));
341 if (cc != sizeof(cellnum))
345 cc = afs_osi_Read(tfile, off, &clen, sizeof(clen));
346 if (cc != sizeof(clen))
350 cellname = afs_osi_Alloc(clen + 1);
354 cc = afs_osi_Read(tfile, off, cellname, clen);
356 afs_osi_Free(cellname, clen + 1);
360 cellname[clen] = '\0';
362 if (afs_cellname_lookup_name(cellname)
363 || afs_cellname_lookup_id(cellnum)) {
364 afs_osi_Free(cellname, clen + 1);
368 cn = afs_cellname_new(cellname, cellnum);
369 afs_osi_Free(cellname, clen + 1);
373 ReleaseWriteLock(&afs_xcell);
378 * Write in-kernel list of cells to disk.
381 afs_cellname_write(void)
383 struct osi_file *tfile;
384 struct cell_name *cn;
387 if (!afs_cellname_dirty || !afs_cellname_inode_set)
389 if (afs_initState != 300)
392 ObtainWriteLock(&afs_xcell, 693);
393 afs_cellname_dirty = 0;
395 tfile = osi_UFSOpen(&afs_cellname_inode);
397 ReleaseWriteLock(&afs_xcell);
401 for (cn = afs_cellname_head; cn; cn = cn->next) {
402 afs_int32 magic, cellnum, clen;
408 magic = AFS_CELLINFO_MAGIC;
409 cc = afs_osi_Write(tfile, off, &magic, sizeof(magic));
410 if (cc != sizeof(magic))
414 cellnum = cn->cellnum;
415 cc = afs_osi_Write(tfile, off, &cellnum, sizeof(cellnum));
416 if (cc != sizeof(cellnum))
420 clen = strlen(cn->cellname);
421 cc = afs_osi_Write(tfile, off, &clen, sizeof(clen));
422 if (cc != sizeof(clen))
426 cc = afs_osi_Write(tfile, off, cn->cellname, clen);
433 ReleaseWriteLock(&afs_xcell);
438 * Cell alias implementation
440 * afs_FindCellAlias: look up cell alias by alias name
441 * afs_GetCellAlias: get cell alias by index (starting at 0)
442 * afs_PutCellAlias: put back a cell alias returned by Find or Get
443 * afs_NewCellAlias: create new cell alias entry
446 struct cell_alias *afs_cellalias_head; /* Export for kdump */
447 static afs_int32 afs_cellalias_index;
448 static int afs_CellOrAliasExists_nl(char *aname); /* Forward declaration */
451 * Look up cell alias by alias name.
453 * \return Found struct or NULL.
455 static struct cell_alias *
456 afs_FindCellAlias(char *alias)
458 struct cell_alias *tc;
460 for (tc = afs_cellalias_head; tc != NULL; tc = tc->next)
461 if (!strcmp(alias, tc->alias))
467 * Get cell alias by index (starting at 0).
468 * \param index Cell index.
469 * \return Found struct or null.
472 afs_GetCellAlias(int index)
474 struct cell_alias *tc;
476 ObtainReadLock(&afs_xcell);
477 for (tc = afs_cellalias_head; tc != NULL; tc = tc->next)
478 if (tc->index == index)
480 ReleaseReadLock(&afs_xcell);
487 * Put back a cell alias returned by Find or Get.
492 afs_PutCellAlias(struct cell_alias *a)
498 * Create new cell alias entry and update dynroot vnode.
504 afs_NewCellAlias(char *alias, char *cell)
506 struct cell_alias *tc;
508 ObtainSharedLock(&afs_xcell, 681);
509 if (afs_CellOrAliasExists_nl(alias)) {
510 ReleaseSharedLock(&afs_xcell);
514 UpgradeSToWLock(&afs_xcell, 682);
515 tc = afs_osi_Alloc(sizeof(struct cell_alias));
516 osi_Assert(tc != NULL);
517 tc->alias = afs_strdup(alias);
518 tc->cell = afs_strdup(cell);
519 tc->next = afs_cellalias_head;
520 tc->index = afs_cellalias_index++;
521 afs_cellalias_head = tc;
522 ReleaseWriteLock(&afs_xcell);
524 afs_DynrootInvalidate();
529 * Actual cell list implementation
531 * afs_UpdateCellLRU: bump given cell up to the front of the LRU queue
532 * afs_RefreshCell: look up cell information in AFSDB if timeout expired
534 * afs_TraverseCells: execute a callback for each existing cell
535 * afs_TraverseCells_nl: same as above except without locking afs_xcell
536 * afs_choose_cell_by_{name,num,index}: useful traversal callbacks
538 * afs_FindCellByName: return a cell with a given name, if it exists
539 * afs_FindCellByName_nl: same as above, without locking afs_xcell
540 * afs_GetCellByName: same as FindCellByName but tries AFSDB if not found
541 * afs_GetCell: return a cell with a given cell number
542 * afs_GetCellStale: same as GetCell, but does not try to refresh the data
543 * afs_GetCellByIndex: return a cell with a given index number (starting at 0)
545 * afs_GetPrimaryCell: return the primary cell, if any
546 * afs_IsPrimaryCell: returns true iff the given cell is the primary cell
547 * afs_IsPrimaryCellNum: returns afs_IsPrimaryCell(afs_GetCell(cellnum))
548 * afs_SetPrimaryCell: set the primary cell name to the given cell name
550 * afs_NewCell: create or update a cell entry
553 struct afs_q CellLRU; /* Export for kdump */
554 static char *afs_thiscell = NULL;
555 afs_int32 afs_cellindex; /* Export for kdump */
558 * Bump given cell up to the front of the LRU queue.
559 * \param c Cell to set.
562 afs_UpdateCellLRU(struct cell *c)
564 ObtainWriteLock(&afs_xcell, 100);
566 QAdd(&CellLRU, &c->lruq);
567 ReleaseWriteLock(&afs_xcell);
571 * Look up cell information in AFSDB if timeout expired
572 * \param ac Cell to be refreshed.
576 afs_RefreshCell(struct cell *ac)
578 if (ac->states & CNoAFSDB)
580 if (!ac->cellHosts[0] || (ac->timeout && ac->timeout <= osi_Time()))
581 afs_LookupAFSDB(ac->cellName);
585 * Execute a callback for each existing cell, without a lock on afs_xcell.
586 * Iterate on CellLRU, and execute a callback for each cell until given arguments are met.
587 * \see afs_TraverseCells
588 * \param cb Traversal callback for each cell.
589 * \param arg Callback arguments.
590 * \return Found data or NULL.
593 afs_TraverseCells_nl(void *(*cb) (struct cell *, void *), void *arg)
595 struct afs_q *cq, *tq;
599 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
602 /* This is assuming that a NULL return is acceptable. */
618 * Execute a callback for each existing cell, with a lock on afs_xcell.
619 * \see afs_TraverseCells_nl
620 * \param cb Traversal callback for each cell.
622 * \return Found data or NULL.
625 afs_TraverseCells(void *(*cb) (struct cell *, void *), void *arg)
629 ObtainReadLock(&afs_xcell);
630 ret = afs_TraverseCells_nl(cb, arg);
631 ReleaseReadLock(&afs_xcell);
637 * Useful traversal callback: Match by name.
639 * \param arg Cell name (compared with cell->cellName).
640 * \return Returns found cell or NULL.
643 afs_choose_cell_by_name(struct cell *cell, void *arg)
649 return strcmp(cell->cellName, (char *)arg) ? NULL : cell;
654 * Useful traversal callback: Match by handle.
656 * \param arg Cell handle (compared with cell->cellHandle).
657 * \return Returns found cell or NULL.
660 afs_choose_cell_by_handle(struct cell *cell, void *arg)
666 return memcmp(cell->cellHandle, (char *)arg, 16) ? NULL : cell;
671 * Useful traversal callback: Match by cell number.
673 * \param arg Cell number (compared with cell->cellNum).
674 * \return Returns found cell or NULL.
677 afs_choose_cell_by_num(struct cell *cell, void *arg)
679 return (cell->cellNum == *((afs_int32 *) arg)) ? cell : NULL;
683 * Useful traversal callback: Match by index.
685 * \param arg Cell index (compared with cell->cellIndex).
686 * \return Returns found cell or NULL.
689 afs_choose_cell_by_index(struct cell *cell, void *arg)
691 return (cell->cellIndex == *((afs_int32 *) arg)) ? cell : NULL;
695 * Return a cell with a given name, if it exists. No lock version.
696 * Does not check AFSDB.
697 * \param acellName Cell name.
698 * \param locktype Type of lock to be used (not used).
702 afs_FindCellByName_nl(char *acellName, afs_int32 locktype)
704 return afs_TraverseCells_nl(&afs_choose_cell_by_name, acellName);
708 * Return a cell with a given name, if it exists.It uses locks.
709 * Does not check AFSDB.
710 * \param acellName Cell name.
711 * \param locktype Type of lock to be used.
715 afs_FindCellByName(char *acellName, afs_int32 locktype)
717 return afs_TraverseCells(&afs_choose_cell_by_name, acellName);
721 * Same as FindCellByName but tries AFSDB if not found.
722 * \param acellName Cell name.
723 * \param locktype Type of lock to be used.
727 afs_GetCellByName(char *acellName, afs_int32 locktype)
731 tc = afs_FindCellByName(acellName, locktype);
733 afs_LookupAFSDB(acellName);
734 tc = afs_FindCellByName(acellName, locktype);
737 afs_cellname_ref(tc->cnamep);
738 afs_UpdateCellLRU(tc);
746 * Return a cell with a given cell number.
747 * \param cellnum Cell number.
748 * \param locktype Lock to be used.
752 afs_GetCell(afs_int32 cellnum, afs_int32 locktype)
755 struct cell_name *cn;
757 tc = afs_GetCellStale(cellnum, locktype);
761 ObtainReadLock(&afs_xcell);
762 cn = afs_cellname_lookup_id(cellnum);
763 ReleaseReadLock(&afs_xcell);
765 tc = afs_GetCellByName(cn->cellname, locktype);
771 * Same as GetCell, but does not try to refresh the data.
772 * \param cellnum Cell number.
773 * \param locktype What lock should be used.
777 afs_GetCellStale(afs_int32 cellnum, afs_int32 locktype)
781 tc = afs_TraverseCells(&afs_choose_cell_by_num, &cellnum);
783 afs_cellname_ref(tc->cnamep);
784 afs_UpdateCellLRU(tc);
790 * Return a cell with a given index number (starting at 0). Update CellLRU as well.
792 * \param locktype Type of lock used.
796 afs_GetCellByIndex(afs_int32 index, afs_int32 locktype)
800 tc = afs_TraverseCells(&afs_choose_cell_by_index, &index);
802 afs_UpdateCellLRU(tc);
807 * Return a cell with a given handle..
809 * \param locktype Type of lock used.
813 afs_GetCellByHandle(void *handle, afs_int32 locktype)
817 tc = afs_TraverseCells(&afs_choose_cell_by_handle, handle);
819 afs_UpdateCellLRU(tc);
824 * Return primary cell, if any.
825 * \param locktype Type of lock used.
829 afs_GetPrimaryCell(afs_int32 locktype)
831 return afs_GetCellByName(afs_thiscell, locktype);
835 * Return number of the primary cell.
837 * Cell number, or 0 if primary cell not found
840 afs_GetPrimaryCellNum(void)
843 afs_int32 cellNum = 0;
844 cell = afs_GetPrimaryCell(READ_LOCK);
846 cellNum = cell->cellNum;
847 afs_PutCell(cell, READ_LOCK);
853 * Returns true if the given cell is the primary cell.
858 afs_IsPrimaryCell(struct cell *cell)
860 /* Simple safe checking */
863 } else if (!afs_thiscell) {
864 /* This is simply a safety net to avoid seg faults especially when
865 * using a user-space library. afs_SetPrimaryCell() should be set
866 * prior to this call. */
867 afs_SetPrimaryCell(cell->cellName);
870 return strcmp(cell->cellName, afs_thiscell) ? 0 : 1;
875 * Returns afs_IsPrimaryCell(afs_GetCell(cellnum)).
880 afs_IsPrimaryCellNum(afs_int32 cellnum)
885 tc = afs_GetCellStale(cellnum, READ_LOCK);
887 primary = afs_IsPrimaryCell(tc);
888 afs_PutCell(tc, READ_LOCK);
895 * Set the primary cell name to the given cell name.
896 * \param acellName Cell name.
897 * \return 0 for success, < 0 for error.
900 afs_SetPrimaryCell(char *acellName)
902 ObtainWriteLock(&afs_xcell, 691);
904 afs_osi_FreeStr(afs_thiscell);
905 afs_thiscell = afs_strdup(acellName);
906 ReleaseWriteLock(&afs_xcell);
911 * Create or update a cell entry.
912 * \param acellName Name of cell.
913 * \param acellHosts Array of hosts that this cell has.
914 * \param aflags Cell flags.
916 * \param fsport File server port.
917 * \param vlport Volume server port.
918 * \param timeout Cell timeout value, 0 means static AFSDB entry.
922 afs_NewCell(char *acellName, afs_int32 * acellHosts, int aflags,
923 char *linkedcname, u_short fsport, u_short vlport, int timeout)
925 struct cell *tc, *tcl = 0;
926 afs_int32 i, newc = 0, code = 0;
928 AFS_STATCNT(afs_NewCell);
930 ObtainWriteLock(&afs_xcell, 103);
932 tc = afs_FindCellByName_nl(acellName, READ_LOCK);
936 tc = afs_osi_Alloc(sizeof(struct cell));
937 osi_Assert(tc != NULL);
938 memset(tc, 0, sizeof(*tc));
939 tc->cellName = afs_strdup(acellName);
940 tc->fsport = AFS_FSPORT;
941 tc->vlport = AFS_VLPORT;
942 AFS_MD5_String(tc->cellHandle, tc->cellName, strlen(tc->cellName));
943 AFS_RWLOCK_INIT(&tc->lock, "cell lock");
947 ObtainWriteLock(&tc->lock, 688);
949 /* If the cell we've found has the correct name but no timeout,
950 * and we're called with a non-zero timeout, bail out: never
951 * override static configuration entries with AFSDB ones.
952 * One exception: if the original cell entry had no servers,
953 * it must get servers from AFSDB.
955 if (timeout && !tc->timeout && tc->cellHosts[0]) {
956 code = EEXIST; /* This code is checked for in afs_LookupAFSDB */
960 /* we don't want to keep pinging old vlservers which were down,
961 * since they don't matter any more. It's easier to do this than
962 * to remove the server from its various hash tables. */
963 for (i = 0; i < AFS_MAXCELLHOSTS; i++) {
964 if (!tc->cellHosts[i])
966 tc->cellHosts[i]->flags &= ~SRVR_ISDOWN;
967 tc->cellHosts[i]->flags |= SRVR_ISGONE;
975 if (aflags & CLinkedCell) {
980 tcl = afs_FindCellByName_nl(linkedcname, READ_LOCK);
985 if (tcl->lcellp) { /* XXX Overwriting if one existed before! XXX */
986 tcl->lcellp->lcellp = (struct cell *)0;
987 tcl->lcellp->states &= ~CLinkedCell;
992 tc->states |= aflags;
993 tc->timeout = timeout;
995 memset(tc->cellHosts, 0, sizeof(tc->cellHosts));
996 for (i = 0; i < AFS_MAXCELLHOSTS; i++) {
997 /* Get server for each host and link this cell in.*/
999 afs_uint32 temp = acellHosts[i];
1002 ts = afs_GetServer(&temp, 1, 0, tc->vlport, WRITE_LOCK, NULL, 0);
1004 ts->flags &= ~SRVR_ISGONE;
1005 /* Set the server as a host of the new cell. */
1006 tc->cellHosts[i] = ts;
1007 afs_PutServer(ts, WRITE_LOCK);
1009 afs_SortServers(tc->cellHosts, AFS_MAXCELLHOSTS); /* randomize servers */
1011 /* New cell: Build and add to LRU cell queue. */
1013 struct cell_name *cn;
1015 cn = afs_cellname_lookup_name(acellName);
1017 cn = afs_cellname_new(acellName, 0);
1020 tc->cellNum = cn->cellnum;
1021 tc->cellIndex = afs_cellindex++;
1022 afs_stats_cmperf.numCellsVisible++;
1023 QAdd(&CellLRU, &tc->lruq);
1026 ReleaseWriteLock(&tc->lock);
1027 ReleaseWriteLock(&afs_xcell);
1029 if (!(aflags & CHush))
1030 afs_DynrootInvalidate();
1035 afs_osi_FreeStr(tc->cellName);
1036 afs_osi_Free(tc, sizeof(struct cell));
1038 ReleaseWriteLock(&tc->lock);
1039 ReleaseWriteLock(&afs_xcell);
1044 * Miscellaneous stuff
1046 * afs_CellInit: perform whatever initialization is necessary
1047 * shutdown_cell: called on shutdown, should deallocate memory, etc
1048 * afs_RemoveCellEntry: remove a server from a cell's server list
1049 * afs_CellOrAliasExists: check if the given name exists as a cell or alias
1050 * afs_CellOrAliasExists_nl: same as above without locking afs_xcell
1051 * afs_CellNumValid: check if a cell number is valid (also set the used flag)
1055 * Perform whatever initialization is necessary.
1060 static char CellInit_done = 0;
1067 AFS_RWLOCK_INIT(&afs_xcell, "afs_xcell");
1068 AFS_RWLOCK_INIT(&afsdb_client_lock, "afsdb_client_lock");
1069 AFS_RWLOCK_INIT(&afsdb_req_lock, "afsdb_req_lock");
1073 afs_cellalias_index = 0;
1077 * Called on shutdown, should deallocate memory, etc.
1082 struct afs_q *cq, *tq;
1085 #ifdef AFS_CACHE_VNODE_PATH
1086 if (cacheDiskType != AFS_FCACHE_TYPE_MEM) {
1087 afs_osi_FreeStr(afs_cellname_inode.ufs);
1090 AFS_RWLOCK_INIT(&afs_xcell, "afs_xcell");
1092 for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
1096 afs_osi_FreeStr(tc->cellName);
1097 afs_osi_Free(tc, sizeof(struct cell));
1102 struct cell_name *cn = afs_cellname_head;
1105 struct cell_name *next = cn->next;
1107 afs_osi_FreeStr(cn->cellname);
1108 afs_osi_Free(cn, sizeof(struct cell_name));
1115 * Remove a server from a cell's server list.
1116 * \param srvp Server to be removed.
1120 afs_RemoveCellEntry(struct server *srvp)
1129 /* Remove the server structure from the cell list - if there */
1130 ObtainWriteLock(&tc->lock, 200);
1131 for (j = k = 0; j < AFS_MAXCELLHOSTS; j++) {
1132 if (!tc->cellHosts[j])
1134 if (tc->cellHosts[j] != srvp) {
1135 tc->cellHosts[k++] = tc->cellHosts[j];
1139 /* What do we do if we remove the last one? */
1141 for (; k < AFS_MAXCELLHOSTS; k++) {
1142 tc->cellHosts[k] = 0;
1144 ReleaseWriteLock(&tc->lock);
1148 * Check if the given name exists as a cell or alias. Does not lock afs_xcell.
1153 afs_CellOrAliasExists_nl(char *aname)
1156 struct cell_alias *ca;
1158 c = afs_FindCellByName_nl(aname, READ_LOCK);
1160 afs_PutCell(c, READ_LOCK);
1164 ca = afs_FindCellAlias(aname);
1166 afs_PutCellAlias(ca);
1174 * Check if the given name exists as a cell or alias. Locks afs_xcell.
1179 afs_CellOrAliasExists(char *aname)
1183 ObtainReadLock(&afs_xcell);
1184 ret = afs_CellOrAliasExists_nl(aname);
1185 ReleaseReadLock(&afs_xcell);
1191 * Check if a cell number is valid (also set the used flag).
1193 * \return 1 - true, 0 - false
1196 afs_CellNumValid(afs_int32 cellnum)
1198 struct cell_name *cn;
1200 ObtainReadLock(&afs_xcell);
1201 cn = afs_cellname_lookup_id(cellnum);
1202 ReleaseReadLock(&afs_xcell);